android中绑定点击事件和按键事件

发布于 2024-12-13 00:33:12 字数 803 浏览 2 评论 0原文

我想要做的是在触发特定按键事件时调用 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 技术交流群。

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

发布评论

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

评论(1

戈亓 2024-12-20 00:33:12

我不确定我是否完全理解你的问题。您只想捕获事件还是想在用户单击View 时执行某些操作?如果是后者,您可以按照以下方法执行此操作...

OnClickListener()

示例:

private Button btn;
btn = (Button) findViewById(R.id.buttonId);
btn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
      // Handle your click events here
   }
});

只需对 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:

private Button btn;
btn = (Button) findViewById(R.id.buttonId);
btn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
      // Handle your click events here
   }
});

Just do the same with the ImageView.

Documentation:

http://developer.android.com/reference/android/view/View.OnClickListener.html

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