android 创建菜单

在ACTIVITY.JAVA 中

package com.example.menutest;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class TestMenuActivity extends Activity{

private MenuInflater mi;

private Button bt1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.test_menu);

mi=new MenuInflater(this);

bt1=(Button) findViewById(R.id.button1);

bt1.setOnClickListener(new OnClickListener() {

 

@Override

public void onClick(View v) {

 

aboutAlert(“click it button”);

 

 

}

});

 

}

 

 

 

 

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

mi.inflate(R.menu.main, menu);

returntrue;

}

 

 

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// TODO Auto-generated method stub

switch (item.getItemId()) {

case R.id.about:

aboutAlert(“this is tell you how to uer R menu”);

break;

case R.id.exit:

exitAlert(“Are you true to exit?”);

break;

 

}

 

returntrue;

}

 

private void exitAlert(String msg) {

// TODO Auto-generated method stub

AlertDialog.Builder builder=new AlertDialog.Builder(this);

builder.setMessage(msg)

.setCancelable(false)

.setPositiveButton(“ok”, new DialogInterface.OnClickListener() {

 

@Override

public void onClick(DialogInterface dialog, int id) {

// TODO Auto-generated method stub

finish();

}

}).setNegativeButton(“cancl”, new DialogInterface.OnClickListener() {

 

@Override

public void onClick(DialogInterface dialog, int id) {

// TODO Auto-generated method stub

return;

 

}

});

AlertDialog alert=builder.create();

alert.show();

 

 

}

 

private void aboutAlert(String ms) {

AlertDialog.Builder builder =new AlertDialog.Builder(this);

builder.setMessage(ms)

.setCancelable(false)

.setPositiveButton(“sure”, new DialogInterface.OnClickListener() {

 

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

 

 

}

});

 

 

在MENU.XML 中

<menuxmlns:android=“http://schemas.android.com/apk/res/android”>

 

<item

        android:id=“@+id/file”

        android:title=“File”>

        <menu>

<group

                android:id=“@+id/noncheckable_group”

                android:checkableBehavior=“none”>

<item

android:id=“@+id/newFile”

android:title=“New”

                android:alphabeticShortcut=“n”

                />

<item

android:id=“@+id/openFile”

android:title=“Open”

                android:alphabeticShortcut=“o”

                />

<item

android:id=“@+id/saveFile”

android:title=“Save”

                android:alphabeticShortcut=“s”

                />

</group>

 

</menu>

</item>

 

 <item 

        android:id=“@+id/edit”

        android:title=“Edit”>

        <menu>

<group

android:id=“@+id/edit_group”

                android:checkableBehavior=“single”>

<item

android:id=“@+id/cut”

android:title=“Cut”

/>

<item

android:id=“@+id/copy”

android:title=“COPY”

/>

<item

android:id=“@+id/past”

android:title=“Past”

/>

</group>

 

</menu>

</item>

  <item 

        android:id=“@+id/help”

        android:title=“Help”>

        <menu>

<group

android:id=“@+id/help_group”>

<item

android:id=“@+id/about”

android:title=“About”

/>

<item

android:id=“@+id/exit”

android:title=“EXIT”

/>

</group>

 

</menu>

</item>  

</menu>

 

AlertDialog alert=builder.create();

 

alert.show();

 

}

 

}

Leave a Comment