首页 > 系统 > Android > 正文

android编程之menu按键功能实现方法

2020-04-11 11:37:27
字体:
来源:转载
供稿:网友

本文实例讲述了android编程之menu按键功能实现方法。分享给大家供大家参考。具体分析如下:

android应用程序可以通过menu按键弹出菜单,现在通过menu按键弹出一个拥有两个选项的菜单.点击第一个按键,文本框会显示"第1个按键",点击第二个按键,文本框会显示"第2个按键"

package com.test_menu;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.*;import android.widget.*;;public class test_menu extends Activity { public static final int ITEM0 = Menu.FIRST; public static final int ITEM1 = Menu.FIRST + 1; private TextView text; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  text = (TextView)findViewById(R.id.textView1); } @Override public boolean onCreateOptionsMenu(Menu menu) {  super.onCreateOptionsMenu(menu);  menu.add(0,ITEM0,0,"button1");  menu.add(0,ITEM1,0,"button2");  menu.findItem(ITEM1);  return true; } public boolean onOptionsItemSelected(MenuItem item) {  switch (item.getItemId())  {  case ITEM0:  {   text.setText("第1个按键");   break;  }  case ITEM1:  {   text.setText("第2个按键");   break;  }  }  return super.onOptionsItemSelected(item); }}

运行效果图:

希望本文所述对大家的Android程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表