如何判断在 countDownTimer() 方法期间用户何时按下按钮?

How to tell when during countDownTimer() method a user presses a button?

我有 countDownTimer() 方法,如果用户在计时器还剩 10 秒或 9 秒时按下按钮,我想打印某个 toast。我试图通过读取 gameButton() 方法中计时器的文本视图来读取时间,但它只打印失败吐司而不是通过吐司。那么为什么我不能这样做呢?还有我怎样才能让它工作?谢谢。

import android.app.Activity;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class GameScreen extends Activity {

    private TextView time;
    private Button start;
    private Button cancel;
    private Button gameButton;
    private CountDownTimer countDownTimer;

    private View.OnClickListener btnClickListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {

            switch(v.getId()){
                case R.id.start_ID :
                    start();
                    break;
                case R.id.cancel :
                    cancel();
                    break;
                case R.id.gameButton_ID :
                    gameButton();
                    break;
            }

        }


    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_screen);

        start = (Button) findViewById(R.id.start_ID);
        start.setOnClickListener(btnClickListener);
        cancel = (Button) findViewById(R.id.cancel);
        cancel.setOnClickListener(btnClickListener);
        time = (TextView) findViewById(R.id.time);
        gameButton = (Button) findViewById(R.id.gameButton_ID);
        gameButton.setOnClickListener(btnClickListener);

    }

    public void start(){
        time.setText("15");
        countDownTimer = new CountDownTimer(15 * 1000, 1000) {
            @Override
            public void onTick(long millsUntilFinished){
                time.setText("" + millsUntilFinished / 1000);

               /* gameButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        // Perform action on click
                        //if(time.getText() == "10" || time.getText() == "9" ){
                            Toast.makeText(new GameScreen(), "This is my Toast message!", Toast.LENGTH_LONG).show();
                        //}
                    }
                }); */
            }

            public void onFinish(){
                time.setText("Done !");
            }
        };
        countDownTimer.start();
    }

    private void cancel(){
        if(countDownTimer != null){
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }

    private void gameButton(){
        if(time.getText() == "10" || time.getText() == "9" ){
            Toast.makeText(getApplicationContext(), "PASS", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(getApplicationContext(), "FAIL", Toast.LENGTH_SHORT).show();
        }
    }

}

使用equals方法而不是==运算符

 time.getText().equals("10") || time.getText().equals("9")

What is the difference between == vs equals() in Java?