1. 程式人生 > 實用技巧 >android 歡迎介面延遲3秒跳轉

android 歡迎介面延遲3秒跳轉

welcome.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    android:background
="@mipmap/img_welcome" > <ImageView android:id="@+id/spalsh_img" android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:scaleType="fitXY" /> <TextView android:id
="@+id/spalsh_textview" android:fitsSystemWindows="true" android:layout_width="60dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_marginTop="20dp" android:layout_marginRight="15dp" android:background="@drawable/z_main_tab_background" android:gravity
="center" android:padding="20dp" android:text="跳過 5" android:textColor="#000000" android:textSize="14sp" /> </RelativeLayout>
z_main_tab_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="#bcFFFFFF"/> <size android:width="25dp" android:height="20dp"/> </shape>

Welecome.java

    
TextView spalshTextview;
private int recLen = 5;//跳過倒計時提示5秒
    private Timer timer = new Timer();
    private Handler handler;
    private Runnable runnable;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_welcome);
        spalshTextview=findViewById(R.id.spalsh_textview);
        spalshTextview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump();
            }
        });
  initData();
        timer.schedule(task, 1000, 1000);//等待時間一秒,停頓時間一秒
    }


    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() { // UI thread
                @Override
                public void run() {
                    recLen--;
                    spalshTextview.setText("跳過 " + recLen);

                    if (recLen < 0) {
                        timer.cancel();
                        spalshTextview.setVisibility(View.GONE);//倒計時到0隱藏字型

                    }
                    if (recLen < 2) {
                        spalshTextview.setEnabled(false);
                    }
                }
            });
        }
    };

    protected void initData() {
        /**
         * 正常情況下不點選跳過
         */
        handler = new Handler();
        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {
                jump();

            }
        }, 5000);//延遲5S後傳送handler資訊
    }

    private void jump() {
        handler.removeCallbacks(runnable);
        if ((boolean) SharedPrefsUtil.getData("isFirst", true)) {
            startActivity(new Intent(WelcomeActivity.this, GuideActivity.class));
            finish();
        } else {
            if (!(boolean) SharedPrefsUtil.getData("isLogin", false)) {
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
            }else{
                startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
            }
            finish();
        }
    }