题目描述:
按下启动按钮,计时器启动,每隔一秒更新到TextView中,直到按下停止按钮。
其他要求:
-
按返回键再启动app,计数值不归0,计数状态为停止。
-
旋转屏幕后,计数值不归0,计数状态和旋转屏幕前相同。
-
界面和其他组件自由发挥。
界面:
代码:
package dhu.cst.liyiqing181310505.chp401;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
Boolean Stop=true;
Button Start;
Button End;
TextView txt;
//这里必须初始化,不然运行的时候日志会报错
testHandler handler = new testHandler();
testThread thread = new testThread();
//记录秒数
Integer count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//开始和停止的按钮,显示计时的文本框
Start=findViewById(R.id.Start);
End=findViewById(R.id.End);
txt=findViewById(R.id.textView);
//接口重写的函数调用
Start.setOnClickListener(new onStart());
End.setOnClickListener(new onEnd());
handler.post(new testThread());
// 检查是否正在重新创建一个以前销毁的实例
if(savedInstanceState!=null){
count = savedInstanceState.getInt("count");
Stop = savedInstanceState.getBoolean("Stop");
txt.setText(String.format(Locale.getDefault(),"%d",count));
}
}
//接口,表示单击
private class onStart implements View.OnClickListener{
@Override
public void onClick(View view) {
Stop =false;
thread.start();
}
}
private class onEnd implements View.OnClickListener {
@Override
public void onClick(View view) {
Stop = true;
}
}
//继承
private class testThread extends Thread {
@Override
public void run() {
while (!Stop) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
Message msg = new Message();
//给成员变量what赋值,接受消息也就是在handleMessage里可以通过判断传入的值不同,做不同的操作
msg.what = 1;
//发送消息
handler.sendMessage(msg);
}
}
}
private class testHandler extends Handler {
@Override
//handleMessage接收消息
public void handleMessage(@NonNull Message msg) {
if (msg.what==1) {
count ++;
txt.setText(String.format(Locale.getDefault(),"%d",count));
}
}
}
//重写onSaveInstanceState
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("count",count);
savedInstanceState.putBoolean("Stop",Stop);
}
/****点返回键不退出程序****/
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent home = new Intent(Intent.ACTION_MAIN);
//暂停线程
Stop = true;
home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
home.addCategory(Intent.CATEGORY_HOME);
startActivity(home);
return true;
}
return super.onKeyDown(keyCode, event);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dhu.cst.liyiqing181310505.chp401">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
这里注意!是使得旋转屏幕不退出线程以及不重新加载屏幕的关键!
android:configChanges="keyboardHidden|orientation|screenSize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
tips:
-
线程的 run() 和 Start() 的区别
详细见:https://www.cnblogs.com/zhaosq/p/10564698.html
https://blog.csdn.net/QQ2899349953/article/details/81772104
-
findViewById一定要用“=”不要用“.”,虽然用“.”在界面内不报错,但是运行的时候会报错。