创建Menu资源
1.在res资源下,创建Resource Directory资源
2.创建menu.xml
3.创建菜单选项
4.重写创建菜单的Java代码
5.重写菜单的单个选项的操作函数
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add"
android:title="增加" />
<item
android:id="@+id/delete"
android:title="删除" />
</menu>
package com.example.firstapplication;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
public boolean onOptionsItemSelected( MenuItem item) {
switch (item.getItemId()){
case R.id.add:
Toast.makeText(this,"you check add",Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
Toast.makeText(this,"you check delete",Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
}