各位大大好,小弟最近剛接觸Andorid
目前程式需要兩個功能,爬了一些文章可是還是沒看懂要如何應用
希望大大可以提供一些意見
1.想用下拉式選單提供秒數,讓旁邊的幻燈片可以按照選擇的秒數變化
2.要在打開程式後,TextView持續更新文字
爬文有看到很多方法,但好像幾乎都是按下按鈕後才能變化
以下是目前的程式碼,可是打開一秒後,app就會閃退
public class test extends AppCompatActivity {
private static final int msgKey1 = 1;
String random_text;
private TextView t1;
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case msgKey1:
t1.setText(testRandom2());
break;
default:
break;
}
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
TextView t1 = findViewById(R.id.t1);
new TimerThread().start();
}
private String testRandom2() {
Random random = new Random();
String test1 = String.valueOf(random.nextInt(999) % (999 - 100 + 1) +
100);
return test1;
}
public class TimerThread extends Thread {
public void run() {
do {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = msgKey1;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (true);
}
}
}