巧妙避免倒计时跳转页面时出现的问题

来源:互联网 发布:动画视频公司 tvc网络 编辑:程序博客网 时间:2024/06/05 04:26

       在Android开发中,当我们实现导航页倒计时跳转时常会出现这样的问题:

        1.计时器正在倒计时的时候点击跳转按钮,会出现跳两次的状况,这是其一;

        2.其二,当计时器正在倒计时的时候按返回键,进入桌面后几秒后又进入应用。

        以下代码解决了这个问题。

方法一:

MainActivity:       

package com.example.daohangye;import android.content.Intent;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.RelativeLayout;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    int count = 6;    private Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            count--;            if (count == 0) {                Intent intent = new Intent(MainActivity.this, Main2Activity.class);                startActivity(intent);            } else {                handler.sendEmptyMessageDelayed(1, 1000);                tv.setText("倒计时:" + count);            }        }    };    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv = (TextView) findViewById(R.id.tv);        handler.sendEmptyMessage(1);        tv.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, Main2Activity.class);                startActivity(intent);                handler.removeMessages(1);            }        });    }    @Override    protected void onDestroy() {        super.onDestroy();        handler.removeMessages(1);        if (handler != null) {            handler = null;        }    }}

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><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"    android:id="@+id/sss"    tools:context="com.example.daohangye.MainActivity"    android:background="@drawable/guide3">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="56dp"        android:id="@+id/tv" /></RelativeLayout>
方法二:
MainActivity:
package com.example.text;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.TextView;import java.util.Timer;import java.util.TimerTask;public class MainActivity extends AppCompatActivity {    private TextView tv;    private Boolean A=true;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv = findViewById(R.id.ss);        tv.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, Main2Activity.class);                startActivity(intent);                A=false;                finish();                Log.e("TAG",A+"");            }        });        //计时器        Timer timer = new Timer();        timer.schedule(new TimerTask() {            @Override            public void run() {                if(A==true) {                    Intent intent = new Intent(MainActivity.this, Main2Activity.class);                    startActivity(intent);                    finish();                }            }        },6000);    }    @Override    protected void onDestroy() {        super.onDestroy();        A=false;    }}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout    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="com.example.text.MainActivity"><TextView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ff3600"    android:id="@+id/ss"    /></LinearLayout>


       

        

原创粉丝点击