简体   繁体   English

用户点击屏幕时如何退出无限while循环?

[英]How to exit an infinite while loop when user taps screen?

I currently have a while loop that starts when a user taps the screen. 我目前有一个while循环,当用户点击屏幕时开始。 The loop is meant to be infinite, and should stop when it senses another screen tap from the user. 该循环是无限的,当它感测到用户再次敲击屏幕时应停止。 (And, if tapped again, it'll start looping again, etc.) I've tried to break the loop in two different ways, with both yielding non-satisfactory results: (而且,如果再次点击它,它将再次开始循环,依此类推。)我试图以两种不同的方式打破循环,但两种方式均产生不令人满意的结果:

METHOD 1: When I try this method, what happens is that the user taps, the loop starts. 方法1:当我尝试这种方法时,发生的事情是用户点击,循环开始。 The user taps again, and that tap doesn't get registered and the loop just keeps on going. 用户再次点击,该点击不会被注册,循环继续进行。

boolean playing = false;
@Override
public boolean onTouch(View v, MotionEvent event) 
{
    if(event.getAction() == MotionEvent.ACTION_DOWN) 
    {
        if(!playing)
        {
            playing = true;
            while(playing)
            {
                //do something here
            }
        }
        else if(playing)
        {
            playing = false;
        }
    }
    return false;
}

METHOD 2: When I try this method, what happens is that the user taps, the loop starts, goes through one iteration, then stops. 方法2:当我尝试这种方法时,发生的事情是用户轻击,循环开始,经历一次迭代,然后停止。

boolean playing = false;
@Override
public boolean onTouch(View v, MotionEvent event) 
{
    if(event.getAction() == MotionEvent.ACTION_DOWN) 
    {
        if(!playing)
        {
            playing = true;         
            whileLoop:
            while(playing)
            {
                //do something here
            }
        }

        if(event.getAction() == MotionEvent.ACTION_DOWN) 
        {
            playing = false;
            break whileLoop;
        }
    }
    return false;
}

I've looked at these questions, but neither helped me with my specific situation: 我看过这些问题,但都没有帮助我解决我的具体情况:

So how do I implement this while loop? 那么如何实现while循环呢? I just want a simple interface where a user's tap would toggle the while loop on/off. 我只想要一个简单的界面,用户点击即可切换while循环的开/关。

Put the infinite loop within a Thread . 将无限循环放在Thread Having it there, you can just have a control variable inside (like playing ), and once tap again and you want to stop, simply set it to false. 在此放置它,您只需在其中包含一个控制变量(如playing ),然后再次轻playing而要停止,只需将其设置为false即可。 The Thread , running paralelly, will be able to handle both events (the tap and the Thread stop). 并行运行的Thread将能够处理两个事件(水龙头和Thread止动件)。

---- EDIT ---- ----编辑----

Example added: 添加示例:

// You'd probably need to store this variable class-wide,
// so you can control when the user has tapped for the first or second time
boolean playing = false;

@Override
public boolean onTouch(View v, MotionEvent event) 
{
    if(event.getAction() == MotionEvent.ACTION_DOWN) 
    {
        if(!playing)
        {
            playing = true;
            new Thread(
              new Runnable() {
                public void run() {
                  while(playing)
                  {
                    //do something here
                  }
                }
              }
            ).start();
        }
        else if(playing)
        {
            playing = false;
            // As you're controlling the playing value in the Thread,
            // setting it here to false would mean that your thread stops too.
        }
    }
    return false;
}

try this: 尝试这个:

boolean playing = false;
Handler handler = new Handler();
Runnable runner = new Runnable() {
    @Override
    public void run() {
        // do something here
        handler.post(this);
    }
};

imageView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN)  {
            if(!playing) {
                playing = true;
                runner.run();
                return true;
            }
            if(playing) {
                playing = false;
                handler.removeCallbacks(runner);
                handler = null;
                runner = null;
                Toast.makeText(context, "NO", Toast.LENGTH_SHORT).show();
                return true;
            }
        }
        return false;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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