仅可使用手写笔单击的按钮 [英] button clickable only with a stylus
问题描述
我有一个按钮,只希望用手写笔单击.我使用了setClickable方法来启用或禁用按钮的单击,但是我怎么能只用笔单击呢?该按钮仅在MotionEvent.TOOL_TYPE_STYLUS
I have a button that I would like clickable only by a stylus. I used the method setClickable to enable or disable the click on the button, but how can I do that is clickable only with a pen? the button should be clickable only when MotionEvent.TOOL_TYPE_STYLUS
我该怎么办?
推荐答案
您可以覆盖 Button
的 onTouchListener
,并在未执行touch事件时立即返回通过 TOOL_TYPE_STYLUS
.要检索此信息,可以使用
you could override the Button
's onTouchListener
, and return immediately it the touch event is not performed through TOOL_TYPE_STYLUS
. To retrieve this information, you can use
getToolType(int pointerX)
从文档
获取给定指针索引的指针的工具类型
Gets the tool type of a pointer for the given pointer index
如果返回 TOOL_TYPE_STYLUS
,则只需检查 MotionEvent
中的 ACTION_DOWN
/ ACTION_UP
,然后调用 performClick()
.例如
if it returns TOOL_TYPE_STYLUS
, then you can simply check the MotionEvent
for the ACTION_DOWN
/ACTION_UP
, and call performClick()
. E.g.
b.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS) {
if (event.getAction() == MotionEvent.ACTION_UP ) {
performClick();
return true;
}
}
return false;
}
});
这篇关于仅可使用手写笔单击的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!