安卓倒计时跳转

//布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Daojishi">
   <TextView
       android:id="@+id/time"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_alignParentEnd="true"
       android:layout_marginTop="34dp"
       android:layout_marginEnd="40dp"
       android:textSize="20sp"
       />
    <ImageView
        android:id="@+id/image"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:src="@drawable/beijing1"/>
</RelativeLayout>

//起始
public class Daojishi extends BaseActivity {
private TextView textView;
private SharedPreferences sp;
private int i=5;

//绑定布局

   @Override
        protected int bindLayout() {
            return R.layout.activity_daojishi;
        }

//绑定控件

   @Override
    protected void initView() {
        textView = BindView(R.id.time);
    }

//操作数据

   @Override
    protected void initData() {
        sp = getSharedPreferences("login2", Context.MODE_PRIVATE);
        if (sp.getBoolean("第一次",false)){
            startActivity(new Intent(Daojishi.this,MainActivity.class));
            finish();
            return;
        }
        SharedPreferences.Editor editor =sp.edit();
        editor.putBoolean("第一次",true);
        editor.commit();
       handler.sendEmptyMessageDelayed(0,1000);
     }

//hander主线程

Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 0:
                i--;
                textView.setText(""+i);
                if (i<=0){
                    Intent intent = new Intent(Daojishi.this,MainActivity.class);
                    startActivity(intent);
                    return;
                }
                handler.sendEmptyMessageDelayed(0,1000);
                break;
        }
    }
};

//设置监听

 @Override
   protected void bindEvent() {
       textView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent(Daojishi.this,MainActivity.class);
               i=0;
               startActivity(intent);
               return;
           }
       });
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43640920/article/details/86559325
今日推荐