菜单XML文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android"><item
陆戚 答备 android:id="@+id/exit"
android:title="退出"/>
</menu>
然后在Activity中重写菜单的方法:
@Overridepublic boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater mf = new MenuInflater(this)
mf.inflate(R.menu.menu,menu) //这里的R.menu.menu为XML文清悉毁件的ID
return super.onCreateOptionsMenu(menu)
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == R.id.exit) finish()
return super.onOptionsItemSelected(item)
}
代码如下:@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menuthis adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu)
return true
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(getApplicationContext(), "菜单1.....", 1).show()
this.finish()//简宏只需要把这个添加到菜单点衡咐含击事件上就可咐笑以了
break
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)