//布局文件
<?xml version="1.0" encoding="utf-8"?>
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:orientation="vertical"
tools:context=".view.MainActivity">
android:id="@+id/tv_time"
android:textSize="50sp"
android:textColor="#EDF5F8F8"
android:layout_marginLeft="500dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:textSize="30sp"
android:id="@+id/tv"
android:layout_marginLeft="300dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
//代码
//Handler
public class MainActivityextends AppCompatActivity {
private TextViewtime;
private int Time=6;
private long mExitTime;
private Handlerhandler=new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==0){
if (Time>1){
Time--;
time.setText(Time+"S");
handler.sendEmptyMessageDelayed(0,1000);
}else {
Intent intent=new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
finish();
}
}
}
};
private TextViewtv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = findViewById(R.id.tv_time);
tv = (TextView) findViewById(R.id.tv);
handler.sendEmptyMessageDelayed(0,1000);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handler.removeCallbacksAndMessages(null);
Intent intent =new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
finish();
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
if ((System.currentTimeMillis()-mExitTime)>2000){
Toast.makeText(this,"双击退出", Toast.LENGTH_SHORT).show();
mExitTime=System.currentTimeMillis();
}else {
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
super.onDestroy();
}