您的位置:首页 > 移动开发 > Android开发

Android初识-菜单的用法

2015-12-16 11:10 351 查看


1.创建menu类型的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="com.example.menudemo.MainActivity" >
<item android:id="@+id/add_items"
android:title="@string/add_name"/>
<item android:id="@+id/remove_items"
android:title="@string/remove_name"/>
</menu>


2.在Activity中绑定menu显示事件

public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

3.在Activity中绑定菜单项点击事件

public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.add_items:
Toast.makeText(getApplicationContext(), "add", Toast.LENGTH_LONG).show();
break;
case R.id.remove_items:
Toast.makeText(getApplicationContext(), "remove", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  menu android