我正在使用按钮创建一个简单的鼹鼠游戏,到目前为止,当按钮被点击进入计数时,我只能获得游戏的分数。如果没有按下按钮,我如何记录未命中数?
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编程,我在尝试做你认为我的时候遇到了很多错误.Belows are到目前为止,我得到的部分代码:
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;
}
} 任何有关代码的帮助将不胜感激。谢谢你。
答案 0 :(得分:0)
我更喜欢实现自定义按钮:
public class WhackAMoleButton extends Button {
public void setMoleActive(boolean active) {
...
}
public int getScore() {
...
}
}
这个想法是从所有可见的“WhackAMoleButton”中收集总得分/计数,并将计数逻辑移动到WhackAMoleButton-Class中。如果按钮从moleActive(true)变为moleActive(false)并且同时没有点击,则可以提出“未被点击” - 计数。