android中绑定点击事件和按键事件
我想要做的是在触发特定按键事件时调用 click() 操作。我可以处理特定对象的 keydown 事件,但是如果有 2 个不同的可点击对象(假设一个是图片,另一个是按钮,并且单击操作的行为不同)怎么办?
所以我只想捕获该事件,然后让 android 表现为单击(而不是按键)。
谢谢
编辑:
我终于找到了解决方案。
步骤:
- 实现 onKeyListener - 调用视图子类对象的performClick() 方法并且要调用onclick。
示例代码:
button.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
view.performClick();
Toast.makeText(
context,
"button onkey - " + i,
Toast.LENGTH_LONG)
.show();
return false; //return true if you want to stop the rest of same key event processing.
}
我担心以后忘记写解决方案。我只写了几个小时。然后我会得到一个新答案的答案。
What I want to do is to call click() action when a specific Key Event fired. I can handle the keydown event for a specific object but what if there are 2 different clickable object ( say one is a picture and the other is a button and click actions behaves different).
So I just want to catch the event and then let android behave as clicked ( not key down ).
thanks
Edit :
I find the solution finally.
steps for it :
-implement onKeyListener
-call the performClick() method of the object that is asubclass of view and you want to call the onclick.
Sample Code :
button.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
view.performClick();
Toast.makeText(
context,
"button onkey - " + i,
Toast.LENGTH_LONG)
.show();
return false; //return true if you want to stop the rest of same key event processing.
}
I am afraid of to forget writing the solution later. I just write for a few hours. Then I will get the answer to a new answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否完全理解你的问题。您只想捕获事件还是想在用户单击
View
时执行某些操作?如果是后者,您可以按照以下方法执行此操作...OnClickListener()
示例:
只需对
ImageView
执行相同操作即可。文档:
http://developer.android. com/reference/android/view/View.OnClickListener.html
I'm not sure I fully understand your question. Do you want to only capture the event or do you want to do something when a user clicks a
View
? Here's how you can do it, if it's the latter...OnClickListener()
Example:
Just do the same with the
ImageView
.Documentation:
http://developer.android.com/reference/android/view/View.OnClickListener.html