如何进行按键检查和UI动作维护?

发布于 2024-10-05 21:20:35 字数 1136 浏览 0 评论 0原文

好吧,我已经制作了一个自定义 Android UI,并且我需要我的 UI 视图来处理此控件。

a)当我按下鼠标按钮(onDown /按下按键)时,视图应该继续做某事(例如:按下按键)

b)一旦我释放鼠标键,视图就会显示(例如:按键已弹起)。

示例流程应该与我按下视图时类似。

输出: (我在视图上按下鼠标按钮并按住

按键已按下

按键已按下

按键已按下

按键已按下

按键已按下

按键已按下

我现在释放鼠标按钮

钥匙已打开。

进一步解释我的问题。我正在发布一个代码片段,逻辑应该在其中

@Override

public boolean onTouchEvent(MotionEvent event) {
    Log.d(TAG, event.getAction());
}

当我按下鼠标按钮时,它会打印“0”(意味着鼠标按下),而如果我离开它,它会在日志上打印“1”,意味着鼠标起来了。这应该有助于逻辑。

感谢您的帮助。


好吧,我尝试过类似的东西

private static int checkValue = 0;

@Override

public boolean onTouchEvent(MotionEvent event) {

    checkValue = event.getAction();

    while (checkValue == 0 ){

    Log.d(TAG, "Pressed");
    checkValue = checkMethod(event.getAction);


}

    private int checkMethod(int test){

    if (checkValue == 0){

      checkValue = 0;
      }

    else checkValue = 1;
}

Well I have made a custom Android UI, and I need my UI view to handle this control.

a) While my mouse button is pressed (onDown / Key pressed) , the view should keep doing a thing (For example : Key is down)

b) As soon as i release my mouse key , the view should say (example: Key is up).

The Sample Flow should be something like when I press the view.

Output:
(I press mouse button on the view and hold it)

Key is down

Key is down

Key is down

Key is down

Key is down

Key is down

(I now release mouse button)

Key is up.

To more explain my problem . I am posting a code snippet where the logic should go

@Override

public boolean onTouchEvent(MotionEvent event) {
    Log.d(TAG, event.getAction());
}

When I press my mouse button , it prints "0" (meaning mouse is down), while if i leave it , it prints on the log "1", meaning mouse is up. That should help with the logic.

Thanks for helping.


Well I have tried something like

private static int checkValue = 0;

@Override

public boolean onTouchEvent(MotionEvent event) {

    checkValue = event.getAction();

    while (checkValue == 0 ){

    Log.d(TAG, "Pressed");
    checkValue = checkMethod(event.getAction);


}

    private int checkMethod(int test){

    if (checkValue == 0){

      checkValue = 0;
      }

    else checkValue = 1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾其所爱 2024-10-12 21:20:35

只需定义一个 OnTouchEventListener...

private OnTouchListener onTouchListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.button) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                //start loop or background task
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                //do something different
            }
        } 
        return true;
    }
} 

...并将其分配给您的按钮。

button.setOnTouchListener(onTouchListener);

当用户触摸按钮时,会触发 ACTION_DOWN 事件。当用户释放按钮时,将触发 ACTION_UP 事件。如果需要,可以在线程中启动一个循环,该循环可以检查布尔变量。我没有检查它是否正确,但它应该看起来像这样:

private OnTouchListener onTouchListener = new OnTouchListener() {

        private boolean flag = false;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (v.getId() == R.id.button) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    flag = true;
                    Thread thread = new Thread(new Runnable() {

                        @Override
                        public void run() {
                            while(flag) {
                                //do something
                            }
                        }
                    });
                    thread.start();
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    flag = false;
                }
            } 
            return true;
        }
    }

Just define an OnTouchEventListener...

private OnTouchListener onTouchListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.button) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                //start loop or background task
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                //do something different
            }
        } 
        return true;
    }
} 

...and assign it to your button.

button.setOnTouchListener(onTouchListener);

When the user touches the button the ACTION_DOWN event is fired. When the user releases the button, the ACTION_UP event is fired. If you want you can start a loop in a thread which can check against a boolean variable. I didn't check if it is correct but it should look like this:

private OnTouchListener onTouchListener = new OnTouchListener() {

        private boolean flag = false;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (v.getId() == R.id.button) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    flag = true;
                    Thread thread = new Thread(new Runnable() {

                        @Override
                        public void run() {
                            while(flag) {
                                //do something
                            }
                        }
                    });
                    thread.start();
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    flag = false;
                }
            } 
            return true;
        }
    }
红玫瑰 2024-10-12 21:20:35

也许您可以在触摸发生时创建并启动一个线程,该线程具有 易失性布尔 stop = false 字段,并在休眠一段时间后在每个循环中检查该值。当触摸结束时,您可以将布尔变量设置为 false。

编辑:添加了一个示例

private MyHandler h;

public void handleDown() {
  System.out.println("touch began");
  h = new MyHandler();
  h.start();
}

public void handleUp() {
  h.pleaseStop = true;
  System.out.println("touch ended");
}

class MyHandler extends Thread {
  volatile boolean pleaseStop = false;

  public void run() {
    while (!pleaseStop) {
      System.out.println("touch is active...");
      Thread.sleep(500);
    }
  }
}

Maybe you could create and start a thread when the touch occurs that has a volatile boolean stop = false field and that checks for this value in every loop after sleeping for a while. When the touch ends, you can set the boolean variable to false.

Edit: added an example

private MyHandler h;

public void handleDown() {
  System.out.println("touch began");
  h = new MyHandler();
  h.start();
}

public void handleUp() {
  h.pleaseStop = true;
  System.out.println("touch ended");
}

class MyHandler extends Thread {
  volatile boolean pleaseStop = false;

  public void run() {
    while (!pleaseStop) {
      System.out.println("touch is active...");
      Thread.sleep(500);
    }
  }
}
要走就滚别墨迹 2024-10-12 21:20:35
checkValue = event.getAction();
while (checkValue == 0 ){

你永远不应该使用神奇的整数。使用 API 定义的常量:

http://developer.android。 com/reference/android/view/MotionEvent.html#ACTION_DOWN

事实上,您的代码甚至没有考虑移动事件;它似乎假设如果不是下降事件,那就是上升事件……这是完全错误的。

查看文档以了解您可以期望的操作值,并针对您感兴趣的特定操作执行正确的操作。例如:

http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP

checkValue = event.getAction();
while (checkValue == 0 ){

You should never ever be using magical integers. Use the constants as defined by the API:

http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN

In fact your code isn't even accounting for move events; it seems to assume that if it isn't a down event it is an up... which is completely wrong.

Look at the documentation to see what action values you can expect, and do the right thing for the specific actions you are interested in. For example up:

http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP

我的影子我的梦 2024-10-12 21:20:35

创建一个由触摸启动的线程,并具有布尔标志以使其保持活动状态。当触摸被释放时,将标志设置为 false,以便线程加入(终止)。

由于线程独立于主逻辑,因此处理速度更快。

Create a thread which is initiated by the touch and have boolean flag to keep it alive. When the touch is released, set the flag to false so the thread joins (terminates).

Because the thread is independent from the main logic, the process is faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文