我正在使用按钮创建一个简单的打鼹鼠游戏,到目前为止,我仅在单击按钮进入计数时才设法获得游戏分数。未按下按钮时如何注册未命中计数?
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
b2 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
final Handler handler = new Handler();
Runnable runnable = new Runnable() { public void run() {
int x1= r1.nextInt(array.length);
int x2= r2.nextInt(array.length);
b1.setText(array[x1].toString());
b1.setText(array[x1].toString());
int rando = (int)((Math.random()) * 2000);
handler.postDelayed(this, rando); //for interval...
}
};
handler.postDelayed(runnable, 2000);
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.equals(b1)){
//Toast.makeText(Random_textviewActivity.this, "Hello", Toast.LENGTH_SHORT).show();
count++;
score.setText(String.valueOf(count));
b1.setText("");
}
else if (v.equals(b2)) {
count++;
score.setText(String.valueOf(count));
b2.setText("");
}
这些是我的代码的一部分,有没有办法注册一个未点击按钮的计数?
更新
好的,我现在已经掌握了整个想法,但是由于我还不太擅长 android 编程,所以在尝试做你认为我的事情时,我遇到了很多错误。以下是部分到目前为止我得到的代码:
b1 = (whackamolebutton) findViewById(R.id.button1);
((whackamolebutton) b1).setmoleactive(this);
public class whackamolebutton extends Button{
public whackamolebutton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}//it insist I must cast a method here which I'm not sure why
//from this point onwards I got confused by the auto fix from eclipse.
public void setmoleactive(Random_textviewActivity random_textviewActivity){
if (boolean active){
count++
}
else{
misscount++
}
}
public int getScore(){
return count;
}
任何有关代码的帮助将不胜感激。在此先感谢您。