Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.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 如何从另一个按钮单击一个按钮开始打破for循环_Java_Android_For Loop - Fatal编程技术网

Java 如何从另一个按钮单击一个按钮开始打破for循环

Java 如何从另一个按钮单击一个按钮开始打破for循环,java,android,for-loop,Java,Android,For Loop,我点击一个按钮就开始执行循环。现在,我希望当单击另一个按钮时,我先前启动的for循环的执行应该停止,并且我希望在该for循环停止执行时检索计数器的值。我怎样才能做到这一点 public void clickStart(View view) { TextView textView = findViewById(R.id.textView); int counter = 0; for (int i=0;i<1500;i++) {

我点击一个按钮就开始执行循环。现在,我希望当单击另一个按钮时,我先前启动的
for循环
的执行应该停止,并且我希望在该
for循环
停止执行时检索计数器的值。我怎样才能做到这一点

public void clickStart(View view) {
        TextView textView = findViewById(R.id.textView);
        int counter = 0;
        for (int i=0;i<1500;i++) {
            counter = counter + 10;
            textView.setText(counter);
        }
    }

public void clickComplete(View view) {
    //Write code to stop executing for loop in clickStart()
}
public void单击开始(查看){
TextView TextView=findViewById(R.id.TextView);
int计数器=0;

对于(int i=0;i,方法是在代码中添加一个方法:

public boolean isRunning()
        {
            return this.keepRunning;
        }
在代码中执行以下操作:

public void clickStart(View view) {
        TextView textView = findViewById(R.id.textView);
        int counter = 0;
        for (int i=0;i<1500;i++) {
            counter = counter + 10;
            textView.setText(counter);
            if(isRunning){
            keepRunning=false;
            break;
            }
        }
    }

public void clickComplete(View view) {
    //Write code to stop executing for loop in clickStart()
      this.keepRunning=true;

}

方法是在代码中添加一个方法:

public boolean isRunning()
        {
            return this.keepRunning;
        }
在代码中执行以下操作:

public void clickStart(View view) {
        TextView textView = findViewById(R.id.textView);
        int counter = 0;
        for (int i=0;i<1500;i++) {
            counter = counter + 10;
            textView.setText(counter);
            if(isRunning){
            keepRunning=false;
            break;
            }
        }
    }

public void clickComplete(View view) {
    //Write code to stop executing for loop in clickStart()
      this.keepRunning=true;

}

因此,这并不是如何从外部停止
for循环
(您已经收到其他用户的答案)的确切答案,但在查看您的问题后,您似乎试图在文本视图中显示一个计数器,该计数器以10秒的增量递增,并使用
for循环

我想指出的是,您的
for循环将在几毫秒内完成执行,也就是说,只要您单击按钮启动循环,您的文本视图将显示计数器的最终值,在您的情况下,该值为
15000

如果我对所需延迟的假设是正确的,您可以使用handler并实现如下所示:

首先在您将要使用它的活动或片段中声明这些全局变量

TextView textView;
final Handler handler = new Handler();
Runnable runnable;
int counter;  //for the +10 counter you were originally using
int count;    //for number of times to execute
Textview需要从多个方法中调用,因此需要全局使用

现在,在
onCreate()
方法中,您可以编写以下代码:

textView = findViewById(R.id.textView);
runnable = new Runnable() {
    public void run() {
        if (count <= 1500) {
            textView.setText(String.valueOf(counter));
            count++;
            counter += 10;
            handler.postDelayed(this, 500);
          //500 milliseconds for half a second delay change this as you require
        } else {
            handler.removeCallbacks(this);
        }
    }
};
因此,每次单击开始计数器时,如果您不希望它在可运行之前在
onCreate
中重置start函数外部的初始化值,则计数器将重置为0。最后,
clickComplete
方法应简单地停止可运行,如下所示:

public void clickComplete(View v) {
        handler.removeCallbacks(runnable);
    }

让我知道这对您是否有帮助。

因此,这并不是如何从外部停止
for loop
的确切答案(您已经收到其他用户的答案),但在看了您的问题之后,您似乎试图在textview中显示一个计数器,该计数器以10秒为单位递增,并带有一些延迟,您正在使用循环的

我想指出的是,您的
for循环将在几毫秒内完成执行,也就是说,只要您单击按钮启动循环,您的文本视图将显示计数器的最终值,在您的情况下,该值为
15000

如果我对所需延迟的假设是正确的,您可以使用handler并实现如下所示:

首先在您将要使用它的活动或片段中声明这些全局变量

TextView textView;
final Handler handler = new Handler();
Runnable runnable;
int counter;  //for the +10 counter you were originally using
int count;    //for number of times to execute
Textview需要从多个方法中调用,因此需要全局使用

现在,在
onCreate()
方法中,您可以编写以下代码:

textView = findViewById(R.id.textView);
runnable = new Runnable() {
    public void run() {
        if (count <= 1500) {
            textView.setText(String.valueOf(counter));
            count++;
            counter += 10;
            handler.postDelayed(this, 500);
          //500 milliseconds for half a second delay change this as you require
        } else {
            handler.removeCallbacks(this);
        }
    }
};
因此,每次单击开始计数器时,如果您不希望它在可运行之前在
onCreate
中重置start函数外部的初始化值,则计数器将重置为0。最后,
clickComplete
方法应简单地停止可运行,如下所示:

public void clickComplete(View v) {
        handler.removeCallbacks(runnable);
    }

如果这对你有什么帮助,请告诉我。

你可以这样做

public boolean stopExecution = false;
public int counter = 0;

public void clickStart(View view) {
       TextView textView = findViewById(R.id.textView);

        counter = 0;
        for (int i=0;i<1500;i++) {
            counter = counter + 10;
            textView.setText(counter);
            // also here use String.valueOf(counter) this would throw sn error in my android studio

            if(stopExecution){
                  stopExecution = false;
                  break;
             }
        }
}

public void clickComplete(View view) {
       stopExecution = true;

       // you can access the "global" value counter from here at whatever value it stopped
       // in case it is still running it will break the execution and you will have the vslue where it stopped
       // but the processor should handle it very fast so maybe you need some delayed loop or ?
)
public boolean stopExecution=false;
公共整数计数器=0;
公共作废单击开始(查看){
TextView TextView=findViewById(R.id.TextView);
计数器=0;

对于(inti=0;i你可以这样做

public boolean stopExecution = false;
public int counter = 0;

public void clickStart(View view) {
       TextView textView = findViewById(R.id.textView);

        counter = 0;
        for (int i=0;i<1500;i++) {
            counter = counter + 10;
            textView.setText(counter);
            // also here use String.valueOf(counter) this would throw sn error in my android studio

            if(stopExecution){
                  stopExecution = false;
                  break;
             }
        }
}

public void clickComplete(View view) {
       stopExecution = true;

       // you can access the "global" value counter from here at whatever value it stopped
       // in case it is still running it will break the execution and you will have the vslue where it stopped
       // but the processor should handle it very fast so maybe you need some delayed loop or ?
)
public boolean stopExecution=false;
公共整数计数器=0;
公共作废单击开始(查看){
TextView TextView=findViewById(R.id.TextView);
计数器=0;

对于(int i=0;i使用线程(Runnable/Handler),当用户单击按钮时,执行不会很长时间结束吗?。cpu将使用easetry使用线程(Runnable/Handler)吞下它当用户点击按钮时,执行不会很长时间吗?。cpu会轻松吞下它。你能更详细地解释处理程序的方式吗?谢谢,我已经添加了处理程序的详细信息谢谢,但我的问题通过尝试接受的解决方案得到了解决。你能更详细地解释处理程序的方式吗?谢谢,我已经添加了处理程序的详细信息细节谢谢,但我的问题通过尝试公认的解决方案得到了解决。这应该是正确的答案。所有for循环答案都很愚蠢。这应该是正确的答案。所有for循环答案都很愚蠢。