所以我在Android Studio中制作游戏,你应该在60秒内尽可能多地点击圆圈。我有特定的TextView用于时间和你取得的分数但是!在第一次点击之前,时间显示“点击开始”,以便计时器在用户需要时启动,而不是自己启动。 我的问题是我在圆圈下面有一个重置按钮,用户可以重新开始回到开始,分数被保存,时间应该说“点击开始”但是计数器只是冻结并且什么都不做,直到用户再次点击它并且它正常开始。我无法弄清楚为什么它不会将它的值改为“点按开始”,当我告诉它在重置按钮的点击监听器中这样做时
代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final Button button = (Button) findViewById(R.id.button);
final TextView txtview2 = (TextView) findViewById(R.id.textView2);
final TextView txtview = (TextView) findViewById(R.id.textView);
updatetime(txtview2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(started == false)
{
reverseTimer(60,txtview2,button,txtview);
started = true;
}
score++;
randnum = r.nextInt(10 - 0) + 0;
if(randnum == 0)
button.setBackground(getResources().getDrawable(R.drawable.circle));
else if(randnum == 1)
button.setBackground(getResources().getDrawable(R.drawable.circle1));
else if(randnum == 2)
button.setBackground(getResources().getDrawable(R.drawable.circle2));
else if(randnum == 3)
button.setBackground(getResources().getDrawable(R.drawable.circle3));
else if(randnum == 4)
button.setBackground(getResources().getDrawable(R.drawable.circle4));
else if(randnum == 5)
button.setBackground(getResources().getDrawable(R.drawable.circle5));
else if(randnum == 6)
button.setBackground(getResources().getDrawable(R.drawable.circle6));
else if(randnum == 7)
button.setBackground(getResources().getDrawable(R.drawable.circle7));
else if(randnum == 8)
button.setBackground(getResources().getDrawable(R.drawable.circle8));
else if(randnum == 9)
button.setBackground(getResources().getDrawable(R.drawable.circle9));
button.setText(String.valueOf(score));
}
});
Button reset_button = (Button) findViewById(R.id.button3);
reset_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Reset(txtview2,button);
started = true;
}
});
}
我的重置功能:
public void Reset(TextView txt,Button button)
{
updatetime(txt);
score = 0;
button.setText(String.valueOf(score));
button.setBackground(getResources().getDrawable(R.drawable.circle));
}
设置高分是刚刚设定得分......
public void updatetime(TextView txt)
{
txt.setText("Time : Tap to start");
}
public void sethighscore(TextView txt)
{
txt.setText("Score: " + String.valueOf(score));
}
我的计时器从60到0:
public void reverseTimer(int Seconds,final TextView tv,final Button button,final TextView txt2){
CountDownTimer CountDownTimer1 = new CountDownTimer(Seconds* 1000+1000, 1000) {
public void onTick(long millisUntilFinished) {
if(clicked)
{
this.cancel();
clicked = false;
if(score > highscore)
sethighscore(txt2);
Reset(tv,button);
started = false;
}
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clicked = true;
}
});
seconds = seconds % 60;
tv.setText("Time : " + String.valueOf(minutes)
+ ":" + String.valueOf(seconds));
}
public void onFinish() {
if(score > highscore)
sethighscore(txt2);
tv.setText("Completed");
Reset(tv,button);
}
}.start();
}
答案 0 :(得分:1)
发生的事情是textView是在主线程中定义的,即UI线程。在其中设置文本不会在另一个线程上工作。你要做的就是创建一个处理程序对象。
Handler handler=new Handler(getApplicationContext().getMainLooper());
现在你想要textView.setText("string");
在那里添加此代码的地方。
handler.post(new Runnable() {
@Override
public void run() {
textView.setText("string");
}
});