Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按钮有两个使用布尔函数的功能?_Java_Android_Button_Boolean - Fatal编程技术网

Java 按钮有两个使用布尔函数的功能?

Java 按钮有两个使用布尔函数的功能?,java,android,button,boolean,Java,Android,Button,Boolean,我刚开始一个新的应用程序项目,但我遇到了一个问题。我想在安卓系统中有一个按钮,按下后,再次按下时,使用布尔键切换到不同的功能。如果您需要什么上下文,我将为倒计时设置一个开始/停止按钮。我想知道这是一个简单的概念,还是一个复杂的过程。谢谢 注意:这不是说长按按钮时如何操作。它指的是第一次按下按钮和第二次按下按钮时的操作。 这是我的密码: TextView timerText; CountDownTimer countDownTimer; Button controlButton; boolean

我刚开始一个新的应用程序项目,但我遇到了一个问题。我想在安卓系统中有一个按钮,按下后,再次按下时,使用布尔键切换到不同的功能。如果您需要什么上下文,我将为
倒计时设置一个开始/停止按钮。我想知道这是一个简单的概念,还是一个复杂的过程。谢谢

注意:这不是说长按按钮时如何操作。它指的是第一次按下按钮和第二次按下按钮时的操作。

这是我的密码:

TextView timerText;
CountDownTimer countDownTimer;
Button controlButton;
boolean timerisActive;

public void controlClick(View view) {

        countDownTimer = new CountDownTimer(timerTime + 100, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                Long minutes = millisUntilFinished / 1000 / 60;
                Long seconds = millisUntilFinished / 1000 - minutes * 60;
                String secondString = seconds.toString();
                String minuteString = minutes.toString();
                if (seconds <= 9) {
                    secondString = "0" + secondString;
                }
                timerText.setText(minuteString + ":" + secondString);
            }

            @Override
            public void onFinish() {
                timerText.setText("0:00");
            }

        }.start();
        if(timerisActive = true){
            countDownTimer.cancel();
            timerText.setText("0:00");
        }
        controlButton.setText("Stop");

    }
TextView timerText;
倒计时倒计时;
按钮控制按钮;
布尔时差;
公共void控件单击(视图){
倒计时=新的倒计时(timerTime+1001000){
@凌驾
公共void onTick(长毫秒未完成){
长分钟=毫秒/千分钟/六十分钟;
长秒=毫秒/千分钟*60;
字符串secondString=seconds.toString();
字符串minuteString=minutes.toString();

如果(秒尝试创建一个新的
boolean
变量,如下所示

 boolean isfirstTime = true;
然后在使用时根据该布尔值第一次或第二次单击时进行检查

使您的
按钮.setOnClickListener
与下面的代码类似

button.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
       if (isfirstTime) {
           // call here first time action
           isfirstTime=false;
       }else {
          // call here second time action
         // make isfirstTime = true; 
         // if you want to perform again first time action after second time use press the button
       }


     }
});

一种可能的方法是,首先创建操作的接口,如:

interface ButtonAction {
    void perform();
}
然后创建一个动作数组,该数组将连续执行。大小为2的数组将为您提供切换动作集,而任何较大的数组都将为您提供切换动作集的灵活性

private ButtonAction[] allButtonActions = ... // Create the concrete action implementations.
并保留活动的索引:

private int activeActionIndex = 0;
然后在按钮中单击事件处理程序:

allButtonActions[activeActionIndex].perform();

// Perform next action at next click.
activeActionIndex++;

// Wrap to first action if reached last action already.
if (activeActionIndex >= allButtonActions.length) {
    activeActionIndex = 0;
}

不,上面的问题是关于点击和长点击的不同操作,而不是在不同的状态下处理点击。谢谢你,安东尼。我编辑了这个问题来澄清这一点。希望这个问题不会被标记。