Handler——倒计时
1.布局
<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=".activity.MainActivity">
<ImageView
android:id="@+id/img"
android:src="@drawable/d"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
2.实现
ublic class SecondActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private int i=3;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView = (TextView)findViewById(R.id.text);
handler.sendEmptyMessageDelayed(0,1000);
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
if (i>0){
i--;
textView.setText(""+i);
handler.sendEmptyMessageDelayed(0,1000);
}else {
Intent intent=new Intent(SecondActivity.this,ThreeActivity.class);
startActivity(intent);
finish();
}
break;
}
}
};
}