我开发了一个程序,当我点击一个开始计算点击次数并显示计数的按钮时,5秒后该按钮是可见的,我想要显示倒计时秒,但它没有显示
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);
}
}
答案 0 :(得分:1)
我建议你看一下CountDownTimer。
此处您修改了更新UI的代码。它只需1秒钟,然后更新TextView 5秒钟。最后,它说" Time up":
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
b.setText("Seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
b.setText("Time up");
b.setEnabled(false);
// Showing user clicks after button is disabled
showClicks();
}
}.start();
答案 1 :(得分:0)
请在现有代码中使用此更改:
mTextView = (TextView)view.findViewById(R.id.total_textview);
mTextView.setVisibility(View.VISIBLE);
button = (Button) view.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) {
setCountDown();
// 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);
getActivity().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);
}
private void setCountDown(){
t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
if (i > 0) {
button.setText("Time Left :" + Integer.toString(i--));
} else {
t.cancel();
}
}
});
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
}
int i = 5实例级
计时器t; // Java util Timer
Hanler处理程序; // Android操作系统处理程序