简体   繁体   English

如何在按下按钮时使一段代码循环,并在不再按下按钮时将其停止?

[英]How can I make a piece of code loop while a Button is pressed and stop it once the Button is no longer Pressed?

I am using the android developer tools in Eclipse, programming in Java, and I need to make an object move across the screen as long as a button is pressed. 我正在Eclipse中使用android开发人员工具,在Java中进行编程,只要按下按钮,我就需要使对象在屏幕上移动。 I've been doing research for hours, and I cannot find any methods to accomplish this. 我已经进行了数小时的研究,但找不到任何方法可以完成此任务。 I've tried running threads, which often crash or seemingly don't execute. 我试过运行线程,这些线程经常崩溃或看似无法执行。 I've also tried an onClickListener which reads the button state and uses it to determine whether or not the button is still pressed. 我还尝试了onClickListener ,该方法读取按钮状态并使用它来确定按钮是否仍处于按下状态。 I'm currently using a while loop, but this just freezes the program. 我当前正在使用while循环,但这只会冻结程序。 I believe that this is the best method, and I've tried to use Thread.sleep in order to limit the number of iterations per second, as I believe that this is the reason it is freezing. 我认为这是最好的方法,并且我尝试使用Thread.sleep来限制每秒的迭代次数,因为我认为这就是冻结的原因。 Am I on the right track or am I way off in left field? 我是在正确的轨道上还是在左边的领域离开? Here is a snippet of code: 这是一段代码:

rightButton.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0) 
        {
            while(arg0.isPressed())
            {
                mover.updateCoordinates(1,  0);
            }
        }

    }); 

Would you try this another method? 您会尝试另一种方法吗?

Firstly declare your button as class variable, declare a Handler and a Runnable: 首先将您的按钮声明为类变量,声明一个Handler和一个Runnable:

private Button rightButton; // You will assign this in onCreate() method
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
    @Override
    public void run() {
        if(rightButton.isPressed())
        {
            // If press state is pressed, move your item and recall the runnable in 100 milliseconds.
            mover.updateCoordinates(1,  0);
            mHandler.postDelayed(mRunnable, 100);
        }
    }
};

Then your button's onClickListener will looks like this: 然后您按钮的onClickListener将如下所示:

rightButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0) 
    {
        // Instead of performing a loop here, just call a runnable, do simple press state checking there.
        mHandler.postDelayed(mRunnable, 100);
    }

}); 

Create a loop that updates the views, waits, and calls itself after it finishes waiting. 创建一个循环,以更新视图,等待并在完成等待后调用自身。 Within the loop, have an animation if statement with a boolean field that move on true and does not move on false. 在循环中,制作一个动画if语句,该语句带有一个布尔字段,该布尔字段按true进行移动,而不会按false进行移动。 Have the onClick method toggle the boolean field's value. 让onClick方法切换布尔字段的值。

Try: 尝试:

myView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent e) {
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            // Start your animation here
        } else if (e.getAction() == MotionEvent.ACTION_UP) {
            // Stop your animation here
        }

       return false;
    }
});

References: 参考文献:

MotionEvent MotionEvent

OnTouchListener OnTouchListener

Alternatively, override onKeyUp and onKeyDown of the View class and check for the KEYCODE_ENTER KeyEvent 或者,重写View类的onKeyUponKeyDown并检查KEYCODE_ENTER KeyEvent

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

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