这是我的代码。它有倒计时器。但在使用OnClickListener放入一些代码后,倒数计时器根本不起作用!但每当我擦除按钮textview的OnClickListener代码时,计时器再次工作。为什么会这样?我是android编程的新手,所以我真的不知道如何解决这个问题。
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
public class WordHunter extends Activity{
protected static final String TAG = null;
String generatedString = " ";
String hello;
TextView text, timer;
ImageButton image1, image2, image3, image4, image5, image6, image7, image8,
image9, image10, image11, image12, image13, image14, image15, image16;
private CountDownTimer countDownTimer;
private boolean timeHasStarted = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.word_hunter);
image1 = (ImageButton) findViewById(R.id.Button1);
image2 = (ImageButton) findViewById(R.id.Button2);
image3 = (ImageButton) findViewById(R.id.Button3);
这是第一个onClickListener;为countdowntimer设置OnClickListener
image1.setOnClickListener(onClickListener);
image2.setOnClickListener(onClickListener);
image3.setOnClickListener(onClickListener);
timer = (TextView) this.findViewById(R.id.timer);
timer.setText("00:01:00");
countDownTimer = new MyCountDownTimer(60000,1000);
final TextView text = (TextView) findViewById(R.id.textHere);
//For loading images
final ImageButton image1 = (ImageButton) findViewById(R.id.Button1);
final ImageButton image2 = (ImageButton) findViewById(R.id.Button2);
final ImageButton image3 = (ImageButton) findViewById(R.id.Button3);
final int[] myPics = { R.drawable.lettera, R.drawable.letterb, R.drawable.letterc, R.drawable.lettere,R.drawable.letteri, R.drawable.lettero, R.drawable.letteru, R.drawable.letterd, R.drawable.letterf, R.drawable.letterg,R.drawable.letterh, R.drawable.letterj, R.drawable.letterk, R.drawable.letterl, R.drawable.letterm, R.drawable.lettern,R.drawable.letterp, R.drawable.letterq, R.drawable.letterqu, R.drawable.letterr, R.drawable.letters, R.drawable.lettert, R.drawable.letterv,R.drawable.letterw, R.drawable.letterx, R.drawable.lettery, R.drawable.letterz };
//Random Image
int rando = (int)(Math.random()* 5);
image1.setImageResource(myPics[rando]);
image1.setId(myPics[rando]);
这是Button TextView的第二个OnClickListener
private OnClickListener myCommoClickListner = new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
...
text.setText(generatedString);
}
};
这是我设置Button TextView OnClickListener(按照指示)
的方式image1.setOnClickListener(myCommoClickListner);
image2.setOnClickListener(myCommoClickListner);
image3.setOnClickListener(myCommoClickListner);
}
这是CoundownTimer OnClickListener
private OnClickListener onClickListener = new OnClickListener(){
public void onClick(View v){
switch(v.getId()){
case R.id.Button1:
if (!timeHasStarted) {
countDownTimer.start();
timeHasStarted = true;
}
break;
case R.id.Button2:
if (!timeHasStarted) {
countDownTimer.start();
timeHasStarted = true;
}
break;
}
}
};
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval){
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
timer.setText(hms);
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
timer.setText("Time's Up!");
}
}
我不知道发生了什么。请帮助。