如何在显示菜单时禁用搜索键和拍照键?

发布于 2024-11-08 04:26:42 字数 3585 浏览 1 评论 0原文

我在我的一个活动中创建了一个菜单,如下所示:

/menu/my_menu_list.xml

   <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/mn_about" android:title="About"
            android:icon="@drawable/about" />
        <item android:id="@+id/mn_display_list" android:title="Display List"
            android:icon="@drawable/display_list" />
    </menu>

还覆盖了这些方法。

 @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.my_menu_list, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.mn_about:
                startActivity(new Intent(this, About.class));
                break;

            case R.id.mn_display_list:
                startActivity(new Intent(this, DisplayList.class));
                break;
            }
        }

这些工作完美。现在我需要在显示此菜单时禁用搜索键和相机键。以下代码对于活动来说效果很好,但我不知道在哪里将此代码放在菜单中。

 @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_CAMERA)) {
                return false;
            }
            if ((keyCode == KeyEvent.KEYCODE_SEARCH)) {
                return false;
            }
            return super.onKeyDown(keyCode, event);
        }

编辑 1

我禁用了整个应用程序的相机。为此,我在清单文件中添加了以下接收器标记:

<receiver android:name=".MyReceiver">
            <intent-filter android:priority="10000">
                <action android:name="android.intent.action.CAMERA_BUTTON" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter android:priority="12000">
                <action android:name="android.intent.action.VOICE_COMMAND" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter android:priority="11000">
                <action android:name="android.intent.action.SEARCH_LONG_PRESS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
    </receiver>

还创建了一个扩展 BroadcastReceiver 的接收器类。

public class Receiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
    abortBroadcast();
    }
}

这解决了相机问题,但不适用于长按搜索和语音拨号器。

编辑2 我还尝试了以下方法来控制 Long_Press_Search 键。但不工作。

    public class MyMenuInflater extends MenuInflater implements OnKeyListener {

        public MyMenuInflater(Context context) {
            super(context);
        }

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                Log.d("KeyPress", "search");
                return true;
            }
            return false;
        }
   }

并将 onCreateOptionsMenu() 更改如下。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MyMenuInflater inflater=new MyMenuInflater(this);
    inflater.inflate(R.menu.my_menu_list, menu);
    return true;
}

它不捕获按键事件。

我需要改变什么?

I've created a menu in my one activity as follows:

/menu/my_menu_list.xml

   <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/mn_about" android:title="About"
            android:icon="@drawable/about" />
        <item android:id="@+id/mn_display_list" android:title="Display List"
            android:icon="@drawable/display_list" />
    </menu>

Also overriden these methods.

 @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.my_menu_list, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.mn_about:
                startActivity(new Intent(this, About.class));
                break;

            case R.id.mn_display_list:
                startActivity(new Intent(this, DisplayList.class));
                break;
            }
        }

Those work perfect. Now I need to disable search key and camera key while displaying this menu. The following code works fine for an activity but I don't know where to put this code for menu.

 @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_CAMERA)) {
                return false;
            }
            if ((keyCode == KeyEvent.KEYCODE_SEARCH)) {
                return false;
            }
            return super.onKeyDown(keyCode, event);
        }

Edit 1

I disabled the camera for whole application. For this, I added following receiver tag in manifest file:

<receiver android:name=".MyReceiver">
            <intent-filter android:priority="10000">
                <action android:name="android.intent.action.CAMERA_BUTTON" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter android:priority="12000">
                <action android:name="android.intent.action.VOICE_COMMAND" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter android:priority="11000">
                <action android:name="android.intent.action.SEARCH_LONG_PRESS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
    </receiver>

Also created a Receiver class that extends BroadcastReceiver.

public class Receiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
    abortBroadcast();
    }
}

This solved for the camera but not working for long press search and voice dialer.

Edit 2
I also tried following way to control Long_Press_Search key. But not working.

    public class MyMenuInflater extends MenuInflater implements OnKeyListener {

        public MyMenuInflater(Context context) {
            super(context);
        }

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                Log.d("KeyPress", "search");
                return true;
            }
            return false;
        }
   }

And changed onCreateOptionsMenu() as follows.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MyMenuInflater inflater=new MyMenuInflater(this);
    inflater.inflate(R.menu.my_menu_list, menu);
    return true;
}

It does not capture the keypress event.

What do I need to change?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文