package com.example.actionbartest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//添加菜单项
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
//对menu栏进行监听
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.add:
Toast.makeText(this, "add", Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();
break;
case R.id.save:
Toast.makeText(this, "save", Toast.LENGTH_SHORT).show();
break;
case R.id.help:
Toast.makeText(this, "help", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
//menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/add"
android:icon="@android:drawable/ic_menu_add"
android:showAsAction="ifRoom"
android:title="@string/add"
android:titleCondensed="@string/add">
</item>
<item
android:id="@+id/delete"
android:icon="@android:drawable/ic_menu_delete"
android:showAsAction="ifRoom"
android:title="@string/delete"
android:titleCondensed="@string/delete">
</item>
<item
android:id="@+id/save"
android:icon="@android:drawable/ic_menu_save"
android:showAsAction="ifRoom"
android:title="@string/save"
android:titleCondensed="@string/save">
</item>
<item
android:id="@+id/help"
android:icon="@android:drawable/ic_menu_help"
android:showAsAction="ifRoom"
android:title="@string/help"
android:titleCondensed="@string/help">
</item>
</menu>
//values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SYcam</string>
<string name="save">保存</string>
<string name="edit">编辑</string>
<string name="add">增加</string>
<string name="delete">删除</string>
<string name="help">帮助</string>
</resources>
//截图如下
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。