0

簡単な設定ページを作りたいので、以下の Google のドキュメントにアクセスしました。

http://developer.android.com/guide/topics/ui/settings.html

新しい Android テスト プロジェクトを作成してから、mainActivity クラス内に PreferenceScreen xml と PreferenceFragment クラスを作成しました。

設定アイコンを押したときに設定画面が表示されるようにしたいのですが、設定という名前のクリックできない項目が 1 つしか表示されません。

私の質問が基本的なものであることはわかっていますが、結果が得られずに機能するように多くのことを試みました。

助けてください?

4

1 に答える 1

1

以下に示すように、アクションバー内のメニューから設定を開く場合:

ここに画像の説明を入力

まず、そのアクティビティの menu.xml ファイル内でメニュー項目を定義する必要があります。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
        <item android:id="@+id/action_settings" android:title="@string/action_settings"
            android:orderInCategory="100" android:showAsAction="never" />
</menu>

次に、メニューを拡張する必要があります。次に、メニュー項目のクリックのイベントを、以下に示すメソッドを使用して処理できます。

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {

            // write code to handle the click for the settings menu item!

            return true;
        }

        return super.onOptionsItemSelected(item);
    }
于 2014-11-19T14:56:33.880 に答える