简体   繁体   中英

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! 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:

 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. 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"); add this code there.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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