Android studioでMenu(メニュー)を使う
5792 ワード
転載は出典を明記してください.http://blog.csdn.net/lb377463323/article/details/46722831
1,resディレクトリの下のmuneフォルダを開く
2,menu_main.xmlに次のコードを追加します.
1,resディレクトリの下のmuneフォルダを開く

2,menu_main.xmlに次のコードを追加します.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/add_item" android:title="Add" />
<item android:id="@+id/remove_item" android:title="Remove" />
menu>
、次のように、MainActivityでonCreatOptionsMenu()メソッドを書き換えます.@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;
}
4、on=OptionsItemSelected()メソッドをMainActivityで書き換えるには、次のようにします. @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.add_item:
Toast.makeText(this,"you clicked Add",Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this,"you clicked Remove",Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
}
、次のようにプログラムを実行します.

