android Menu是啥呢,就是按手机上那个菜单按钮对应的东西,一般我们都会使用这个东西完成一些功能:
关于核结束时最普遍的,就是在使用程序过程中有事情需要马上退出程序或者查看如何操作等,在这里我们就
是实现这么一个功能。考虑到点关于希望出现跳转页面,我们这里就新建一个Activity,使用Intent实例的setClas
s方法实现跳转的功能,关于新建Activity前面已经提到过注意事项了,主要是3个:1,注册Activity到AndroidM
enifest.xml文件 2,创建对应Activity的xml文件,用于setContentView调用显示。3,添加新的Activity中使用的
string元素到string.xml文件中
下面是代码部分:(这里没有贴出About的代码)
public class MenuDemo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView t=new TextView(this); setContentView(R.layout.main);//显示main.xml中的元素
Button button =(Button) findViewById(R.id.ok);//按钮 button.setOnClickListener(ok); //按钮监听 } private OnClickListener ok=new OnClickListener(){//监听实现 public void onClick(View arg0) { openOptionsDialog();//打开Alert对话框}};
private void openOptionsDialog(){// TODO Auto-generated method stub new AlertDialog.Builder(this).setTitle("关于BMI").setMessage("BMI call,U can Do This").setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stub } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stub Uri uri=Uri.parse("http://androidbmi.googlecode.com/"); Intent intent=new Intent(Intent.ACTION_VIEW,uri); startActivity(intent);//使用默认浏览器打开URL } }).show();}
protected static final int MENU_ABOUT=Menu.FIRST;//定义Menu上的元素,根据需要可多添加几个 protected static final int MENU_END=Menu.FIRST+1; public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); menu.add(0,MENU_ABOUT,0,R.string.menu_about);//把定义的元素添加到Menu上 menu.add(0,MENU_END,0,R.string.menu_end); return true; }; public boolean onOptionsItemSelected(MenuItem item){//通过ItemId识别炒作的Menu元素 super.onOptionsItemSelected(item); switch(item.getItemId()) { case MENU_ABOUT: { Intent intent=new Intent(); intent.setClass(hello.this,About.class);//切换显示页面为About的Activity startActivity(intent); break; } case MENU_END: { finish(); break; } } return true; };}