本文实例讲述了android创建optionsmenu的方法。分享给大家供大家参考。具体实现方法如下:
一、用xml配置
在res文件夹下新建menu文件夹,menu下新建options_menu.xml文件,加入代码
1、
1
2
3
4
5
6
7
8
9
|
< menu xmlns:android = "http://schemas.android.com/apk/res/android" >
< item android:id = "@+id/about"
android:title = "关于"
android:checkable = "true" />
< item android:id = "@+id/choose"
android:title = "选择" />
< item android:id = "@+id/quit"
android:title = "QUIT" />
</ menu >
|
2、activity类中加入代码
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
|
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return super .onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem aitem)
{
super .onOptionsItemSelected(aitem);
switch (aitem.getItemId())
{
case R.id.about:
setTitle( "你按下了'关于'菜单项" );
progressBarHorizontal.setProgress( 200 );
openOptionsDialog();
break ;
case R.id.choose:
setTitle( "你按下了'选择'菜单项" );
openOptionsChooseDialog();
break ;
case R.id.quit:
setTitle( "你按下了'离开'菜单项" );
finish();
break ;
}
return true ;
}
private void openOptionsDialog()
{
new AlertDialog.Builder( this )
.setTitle(R.string.app_about)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();
}
private void openOptionsChooseDialog()
{
new AlertDialog.Builder( this )
.setTitle(R.string.str_alert)
.setItems(R.array.items_irdc_dialog, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{ /*
String[] aryShop = getResources().getStringArray(R.array.items_irdc_dialog);
new AlertDialog.Builder(this)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();*/
}
})
.show();
}
|
OK。。。
二、直接上代码:
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
|
public static final int ABOUT_ID = Menu.FIRST;
public static final int CHOOSE_ID = Menu.FIRST + 1 ;
public static final int EXIT_ID = Menu.FIRST + 2 ;
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add( 0 , ABOUT_ID, 0 , R.string.app_about);
menu.add( 0 , CHOOSE_ID, 0 , R.string.str_choose);
menu.add( 0 , EXIT_ID, 0 , R.string.str_exit);
return super .onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem aitem)
{
super .onOptionsItemSelected(aitem);
switch (aitem.getItemId())
{
case ABOUT_ID:
setTitle( "你按下了'关于'菜单项" );
progressBarHorizontal.setProgress( 200 );
openOptionsDialog();
break ;
case CHOOSE_ID:
setTitle( "你按下了'选择'菜单项" );
openOptionsChooseDialog();
break ;
case EXIT_ID:
setTitle( "你按下了'离开'菜单项" );
finish();
break ;
}
return true ;
}
private void openOptionsDialog()
{
new AlertDialog.Builder( this )
.setTitle(R.string.app_about)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();
}
private void openOptionsChooseDialog()
{
new AlertDialog.Builder( this )
.setTitle(R.string.str_alert)
.setItems(R.array.items_irdc_dialog, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{ /*
String[] aryShop = getResources().getStringArray(R.array.items_irdc_dialog);
new AlertDialog.Builder(this)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();*/
}
})
.show();
}
|
希望本文所述对大家的Android程序设计有所帮助。