简体   繁体   English

TextView拒绝更改文本

[英]TextView refuses to change text

So I'm making a game in Android Studio where you are supposed to tap the circle as many times as you can in 60 seconds.I have specific TextView's for time and the score that you achieved BUT! 因此,我正在Android Studio中制作一个游戏,您应该在60秒内尽可能多地点击圆圈。我有特定的TextView作为时间和您获得的分数! before the first tap the time says "Tap to start" so that the timer starts when the user want's and not on it's own. 在第一次点击之前,时间会显示“点击开始”,以便计时器在用户需要时(而不是自己)启动。 My problem is that i have a Reset button under the circle where the user can restart back to the start the score is saved and the time should say "Tap to start" but the counter just freezes and does nothing until the user tap's again and it start's as normal.I can't figure out why it won't change it's value to "Tap to start" when i told it to do so in the On click listener from the Reset button 我的问题是,我在圆圈下有一个“重置”按钮,用户可以在其中重新启动,然后保存到比分中,并且时间应显示为“点击以开始”,但计数器只是冻结,什么也没做,直到用户再次点击它为止开始是正常的。当我在“重置”按钮的“单击时侦听器”中告诉它将其值更改为“点击以开始”时,我不知道为什么它不会将其值更改为“点击以开始”

the code: 编码:

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;
            }
        });
    }

My reset function: 我的重置功能:

public void Reset(TextView txt,Button button)
    {
        updatetime(txt);
        score = 0;
        button.setText(String.valueOf(score));
        button.setBackground(getResources().getDrawable(R.drawable.circle));
    }

set high score is actuall just set score... 设定高分实际上就是设定分...

    public void updatetime(TextView txt)
    {
        txt.setText("Time : Tap to start");
    }
    public void sethighscore(TextView txt)
    {
        txt.setText("Score: " + String.valueOf(score));
    }

My timer that counts from 60 to 0: 我的计时器从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();
    }

What happening is that textView is defined in main thread ie UI thread. 发生的事情是textView是在主线程(即UI线程)中定义的。 Setting text in it wont work on another thread.what u have to do is make a handler object. 在其中设置文本将无法在另一个线程上工作。您要做的是制作一个处理程序对象。

Handler handler=new Handler(getApplicationContext().getMainLooper());

now the place where u want to do the textView.setText("string"); 现在是您要执行textView.setText("string"); add this code there. 在此处添加此代码。

 handler.post(new Runnable() {
                        @Override
                        public void run() {
                          textView.setText("string");
                          }
                    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM