2

次のように、1 つのアクティビティでメニューを作成しました。

/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 を拡張する Receiver クラスも作成しました。

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;
}

キー押下イベントはキャプチャされません。

何を変更する必要がありますか?

4

0 に答える 0