安卓欢迎界面的编写

来源:互联网 发布:淘宝抢货神器 编辑:程序博客网 时间:2024/06/06 02:01

      首先制作一个欢迎界面的布局文件,命名为welcome.xml,相关代码如下:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="@drawable/welcome"
        >
     </LinearLayout>

     接下来,新建一个主活动,命名为Welcome。然后再AndroidManifest.xml文件中,添加该活动,并且设置为启动项。关键代码如下:

    <activity
            android:name=".Welcome"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />    
            </intent-filter>
        </activity>

接下来,在活动Welcome里面写关键代码:

                 super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
Thread timer=new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent welcomeIntent=new Intent();
welcomeIntent.setClass(Welcome.this, Login.class);//欢迎界面之后进入的是登录界面
startActivity(welcomeIntent);
}
}
};
timer.start();
}

@Override
protected void onPause() {  //这个方法的作用是是的欢迎界面为一次性显示,当用户按返回键时不会再出现欢迎界面。而是会直接退出软件。
// TODO Auto-generated method stub
super.onPause();
finish();
}   


0 0