關(guān)于Menu的幾個(gè)重要的方法,都是Activity的方法;
- onCreateOptionsMenu(Menu menu)
每次Activity一創(chuàng)建就會(huì)執(zhí)行,一般只執(zhí)行一次; - onPrepareOptionsMenu(Menu menu)
每次menu被打開時(shí),該方法就會(huì)執(zhí)行一次; - onOptionsItemSelected(MenuItem item)
每次menu菜單項(xiàng)被點(diǎn)擊時(shí),該方法就會(huì)執(zhí)行一次; - invalidateOptionsMenu()
刷新menu里的選項(xiàng)里內(nèi)容,它會(huì)調(diào)用onCreateOptionsMenu(Menu menu)方法 - onCreateContextMenu()
創(chuàng)建控件綁定的上下文菜單menu,根據(jù)方法里的View參數(shù)識別是哪個(gè)控件綁定 - onContextItemSelected(MenuItem item)
點(diǎn)擊控件綁定的上下菜單menu的內(nèi)容項(xiàng)
一、Menu標(biāo)題菜單項(xiàng)創(chuàng)建
- 在Android工程目錄下的res包下,右鍵選擇New菜單項(xiàng)下的Android resource directory,并以menu命名文件夾的名字;
- 右鍵menu文件夾,選擇Menu resource file選項(xiàng),創(chuàng)建main_menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/openMenu" android:title="打開"/>
<item android:id="@+id/closeMenu" android:title="關(guān)閉"/>
</menu>
效果圖

Menu.png
- 在Activity中重寫三個(gè)方法
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.i("dayang","打開Menu時(shí)執(zhí)行該方法");
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i("dayang","onCreateOptionsMenu");
//創(chuàng)建Menu菜單
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.i("dayang","選擇列表項(xiàng)時(shí)執(zhí)行------------");
//對菜單項(xiàng)點(diǎn)擊內(nèi)容進(jìn)行設(shè)置
int id = item.getItemId();
if (id == R.id.openMenu) {
Toast.makeText(this,"打開文件",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,"關(guān)閉文件",Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
效果圖

Menu點(diǎn)擊.png
二、創(chuàng)建上下文菜單項(xiàng)
registerForContextMenu(View view)綁定相關(guān)點(diǎn)擊控件長按出菜單項(xiàng)
效果圖1

Menu長按圖片1.jpg
效果圖2

Menu長按圖片2.jpg
1.創(chuàng)建main_menu2.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu1" android:title="呵呵噠"></item>
<item android:id="@+id/menu2" android:title="滴滴"></item>
<item android:id="@+id/menu3" android:title="喔喔"></item>
</menu>
2.創(chuàng)建的Activity的布局xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<EditText
android:id="@+id/etMenu1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="快來長按我" />
<EditText
android:id="@+id/etMenu2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="快來長按我2" />
</LinearLayout>
3.創(chuàng)建的Activity,設(shè)置控件長按事件
public class ContextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_context);
EditText et1= (EditText) findViewById(R.id.etMenu1);
EditText et2= (EditText) findViewById(R.id.etMenu2);
registerForContextMenu(et1);
registerForContextMenu(et2);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if(v.getId()==R.id.etMenu1){
getMenuInflater().inflate(R.menu.main_menu,menu);
}else if(v.getId()==R.id.etMenu2){
getMenuInflater().inflate(R.menu.main_menu2,menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return true;
}}