天天看点

安卓 延时进入页面(新手笔记-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>