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

标签 android countdowntimer

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

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?

关于android - 如何判断在 countDownTimer() 方法期间用户何时按下按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42285722/

相关文章:

Android SSL 证书固定

java - 如何使用 Android CountDownTimer 倒计时

javascript - 单击按钮后需要创建倒计时

android - 如何使用 Firebase ML Kit 自定义模型识别数字

android - 对于从后台线程定期更新并跨多个 Activity 维护的 ListView,什么是好的方法?

java - 从 vision api 预览条形码扫描器的大小

java - 如何在倒数计时器中实现暂停和恢复方法

javascript - 如何在 WebView 中添加 JavaScript 函数并稍后在提交 reCAPTCHA 时从 HTML 中调用它

jquery - 计算器给出错误答案的时间

android - 需要一个准确且可以暂停的 CountdownTimer android