如何进行按键检查和UI动作维护?
好吧,我已经制作了一个自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需定义一个 OnTouchEventListener...
...并将其分配给您的按钮。
当用户触摸按钮时,会触发 ACTION_DOWN 事件。当用户释放按钮时,将触发 ACTION_UP 事件。如果需要,可以在线程中启动一个循环,该循环可以检查布尔变量。我没有检查它是否正确,但它应该看起来像这样:
Just define an OnTouchEventListener...
...and assign it to your button.
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:
也许您可以在触摸发生时创建并启动一个线程,该线程具有 易失性布尔 stop = false 字段,并在休眠一段时间后在每个循环中检查该值。当触摸结束时,您可以将布尔变量设置为 false。
编辑:添加了一个示例
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
你永远不应该使用神奇的整数。使用 API 定义的常量:
http://developer.android。 com/reference/android/view/MotionEvent.html#ACTION_DOWN
事实上,您的代码甚至没有考虑移动事件;它似乎假设如果不是下降事件,那就是上升事件……这是完全错误的。
查看文档以了解您可以期望的操作值,并针对您感兴趣的特定操作执行正确的操作。例如:
http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP
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
创建一个由触摸启动的线程,并具有布尔标志以使其保持活动状态。当触摸被释放时,将标志设置为 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.