Tips

onPrepareOptionsMenu()を用いて表示させるオプションメニューを変更するサンプルアプリ

OptionMenuを表示させるAndroidサンプルアプリです!

※はじめに
この記事はAndroidアプリの開発が、初心者であるという方のための記事です。
そのため、なるべく複雑な説明は避け、コピー&ペイストですぐに動くものをご紹介します。
JavaやAndroidを理解されている方で細かい説明が必要な方は、当ブログ内の連載記事である「Android Tips」をご覧ください。

このアプリはCheckBoxの状態に応じて、onPrepareOptionsMenu()を用いて、表示されるオプショントメニューを切り替えるというものです。

アプリ完成図

1

1

1

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.example.optionmenu2;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
    RadioGroup radioGroup;
    RadioButton radioButton;
    RadioButton checkRadioButton;
    int checkedId;
 
    private String[] os = { "Windows", "Linux", "Mac" };
    private String[] office = { "Word", "PowerPoint", "Excel", "Outlook" };
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
 
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
 
                //選択されたRadioButtonのIDを取得
                radioButton = (RadioButton) findViewById(checkedId);
                Toast.makeText(MainActivity.this,
                        radioButton.getText() + "を選択しました。", Toast.LENGTH_LONG)
                        .show();
            }
        });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        checkedId = radioGroup.getCheckedRadioButtonId();
 
        //メニュー項目を作成
        //add()の第1引数でRadioButtonをGroup分け
 
        //1つ目のRadioButtonが選択されているときに表示させるオプションメニュー
        menu.add(0, 0, 0, os[0]);
        menu.add(0, 10, 1, os[1]);
        menu.add(0, 20, 2, os[2]);
 
        //2つ目のRadioButtonが選択されているときに表示させるオプションメニュー
        menu.add(1, 0, 0, office[0]);
        menu.add(1, 10, 1, office[1]);
        menu.add(1, 20, 2, office[2]);
        menu.add(1, 30, 3, office[3]);
 
        return true;
    }
 
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        checkedId = radioGroup.getCheckedRadioButtonId();
 
        //setGroupVisible()の第1引数には、上で作成した項目のグループIDを指定
        //setGroupVisible()の第2引数には、そのグループの有効無効を指定
 
        if (checkedId == R.id.radioButton1) {
            menu.setGroupVisible(0, true);
            menu.setGroupVisible(1, false);
        } else {
            menu.setGroupVisible(0, false);
            menu.setGroupVisible(1, true);
        }
 
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
 
        checkedId = radioGroup.getCheckedRadioButtonId();
 
        //getTitle()を用いて、選択された項目(item)のタイトル(文字列)を取得
 
        if (checkedId == R.id.radioButton1) {
            if (item.getItemId() == 0) {
                Toast.makeText(this, item.getTitle() + "が選択されました。",
                        Toast.LENGTH_SHORT).show();
            } else if (item.getItemId() == 10) {
                Toast.makeText(this, item.getTitle() + "が選択されました。",
                        Toast.LENGTH_SHORT).show();
            } else if (item.getItemId() == 20) {
                Toast.makeText(this, item.getTitle() + "が選択されました。",
                        Toast.LENGTH_SHORT).show();
            }
 
        } else if (checkedId == R.id.radioButton2) {
            if (item.getItemId() == 0) {
                Toast.makeText(this, item.getTitle() + "が選択されました。",
                        Toast.LENGTH_SHORT).show();
            } else if (item.getItemId() == 10) {
                Toast.makeText(this, item.getTitle() + "が選択されました。",
                        Toast.LENGTH_SHORT).show();
            } else if (item.getItemId() == 20) {
                Toast.makeText(this, item.getTitle() + "が選択されました。",
                        Toast.LENGTH_SHORT).show();
            } else if (item.getItemId() == 30) {
                Toast.makeText(this, item.getTitle() + "が選択されました。",
                        Toast.LENGTH_SHORT).show();
            }
        }
 
        return true;
    }
}

activity_main.xml

今回の画面の定義(activity_main.xml)はデフォルトのtextviewにidのみ追加しています。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    tools:context=".MainActivity" >
 
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
 
        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:checked="true"
            android:text="OS" />
 
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/radioButton1"
            android:layout_below="@+id/radioButton1"
            android:text="Office" />
    </RadioGroup>
 
</RelativeLayout>

これでOKです。

Androidアプリ開発の必須知識!JAVAプログラミングを学べる連載リンク

はじめてのJAVA 連載

Recent News

Recent Tips

Tag Search