0
我開發一個程序,當我點擊一個按鈕開始計算點擊次數並顯示計數時,並且在5秒後該按鈕是可以理解的,並且我也想顯示倒計時秒數不顯示同時在一個按鈕上執行三個onClick操作
mTextView = (TextView) findViewById(R.id.total_textview);
mTextView.setVisibility(View.VISIBLE);
Button button = (Button) findViewById(R.id.count_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
final Button b = (Button)v;
if (clicks == 0){
// Means its the first time that a user click the button
// Start a thread that is going to disable the button after 5 seconds from first click
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable() {
@Override
public void run() {
b.setText("Time up");
b.setEnabled(false);
// Showing user clicks after button is disabled
showClicks();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
// Here we are just counting . . . . including the first click
countClicks();
}
});
}
private void countClicks(){
++clicks;
mTextView.setText(Integer.toString(clicks));
// You can update your text view here
}
private void showClicks(){
mTextView.setText(String.valueOf(clicks)+"Clicks");
mTextView.setVisibility(View.VISIBLE);
}
}