天天看點

安卓 延時進入頁面(新手筆記-1)

本部落格就是一個新手筆記,望dalao們看看、笑笑就好。

其實延時進入頁面就是把頁面顯示兩秒後再進入下一個頁面(目前我接觸到的方法就是這一種,也不知道有沒有其他方法),下面為代碼。

這裡是一個welcome_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/welcome_img"
        android:src="@drawable/welcome_img"
        />

</android.support.constraint.ConstraintLayout>
           

Welcome_Activity的代碼如下:

public class WelcomeActivity extends Activity {

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //這裡設定了不顯示app裡面系統自帶的title
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.welcome_layout);

            //使用一個多線程來處理延時問題,這裡是使用他來實作延時2秒進入首頁面
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                    startActivity(intent);
                    //如果不加finish跳轉過去之後我們再次點選傳回鍵他會傳回到這個頁面
                    finish();
                }
            },2000);
        }
}
           

對于下面這條語句失效的問題可以檢視連結:http://blog.csdn.net/bingjianIT/article/details/51706518

requestWindowFeature(Window.FEATURE_NO_TITLE);
           

之後我在裡面又重寫了onKeyDown方法,作用在新手筆記-3中有,其實(個人了解)就是實作點選按鍵之後進行的操作,這裡重寫這個方法避免使用者在歡迎界面就點選傳回鍵退出程式,顯得程式比較嚴謹嘛=。=

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //避免點選傳回鍵之後退出界面
        if(keyCode == KeyEvent.KEYCODE_BACK ){
            return true;
        }
        return false;
    }
           

由于我們設定了Welcome這個Activity作為初始頁面,我們需要更改AndroidMainfest中的配置:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
        </activity>
        <activity android:name=".WelcomeActivity">
            <intent-filter>
                <!--下面這兩個決定了第一個頁面,是以我們移動了一下,不把他放在MainActivity中-->
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>