看其它应用软件时使用了很多的弹出菜单,蛮好的,顺带学习了下,写了个小DEMO,里面穿插的讲了下系统级的dialog使用小巧门,希望与大家交流学习。。下面来看代码:
- package com.xiaoma.popupmenu.demo;
- import android.app.Activity;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.PopupMenu;
- import android.widget.Toast;
- import android.widget.PopupMenu.OnMenuItemClickListener;
- /**
- * @Title: PopupMenuDemoActivity.java
- * @Package com.xiaoma.popupmenu.demo
- * @Description: 弹出式菜单测试类
- * @author MZH
- */
- public class PopupMenuDemoActivity extends Activity implements OnClickListener,OnMenuItemClickListener{
- private Button show;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- /*
- * 初始化实现
- */
- private void init(){
- show = (Button)findViewById(R.id.button1);
- show.setOnClickListener(this);
- }
- /*
- * 添加监听器(non-Javadoc)
- * @see android.view.View.OnClickListener#onClick(android.view.View)
- */
- @Override
- public void onClick(View v) {
- if(v.getId() == R.id.button1){
- showPopup(v);
- }
- }
- /*
- * 弹出对话框实现
- */
- private void showPopup(View v){
- PopupMenu popup = new PopupMenu(getApplicationContext(),v);
- popup.setOnMenuItemClickListener(this);
- MenuInflater inflater = popup.getMenuInflater();
- inflater.inflate(R.menu.menutest, popup.getMenu());
- popup.show();
- }
- /*
- * 给选择的项添加监听器(non-Javadoc)
- */
- @Override
- public boolean onMenuItemClick(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.new_game:
- Log.i("KKK", "奇怪");
- ShowDialog(item.getTitle());
- break;
- case R.id.help:
- ShowDialog(item.getTitle());
- break;
- case R.id.file:
- ShowDialog(item.getTitle());
- break;
- case R.id.create_new:
- ShowDialog(item.getTitle());
- break;
- case R.id.open:
- ShowDialog(item.getTitle());
- break;
- default:
- break;
- }
- return false;
- }
- /*
- * 弹出消息方法实现
- * 因为安卓系统中也自带这样一个临时弹出dialog的方法
- * showDialog(int id),所以小马在此处违背了下一般的命名
- * 规则,起名:ShowDialog(),下面顺带讲下系统自带的临时dialog的实现
- */
- private void ShowDialog(CharSequence ch){
- Toast.makeText(getApplicationContext(), ch, Toast.LENGTH_SHORT).show();
- }
- /**
- * 在使用系统级自带的showDialog(int id)方法时,必须重载下面这个方法哦
- */
- @Override
- protected Dialog onCreateDialog(int id) {
- ProgressDialog dialog = new ProgressDialog(this);
- dialog.setMessage("小马临时放的字符串一");
- return dialog;
- }
- /**
- * 在使用系统级自带的showDialog(int id,int id, Bundle args)方法时,
- * 必须重载下面这个方法
- * id:dialog的ID
- * args为提供给showDialog的第二个方法
- */
- @Override
- protected Dialog onCreateDialog(int id, Bundle args) {
- ProgressDialog dialog = new ProgressDialog(this);
- dialog.setMessage("小马临时放的字符串二");
- return dialog;
- }
- }
加载的菜单布局如下:
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android" >
- <item android:id="@+id/new_game"
- android:icon="@drawable/ic_launcher"
- android:title="新游戏"
- android:showAsAction="ifRoom"/>
- <item android:id="@+id/help"
- android:icon="@drawable/ic_launcher"
- android:title="帮助" />
- <item android:id="@+id/file"
- android:title="嵌套" >
- <menu>
- <item android:id="@+id/create_new"
- android:title="开始新游戏" />
- <item android:id="@+id/open"
- android:title="开始" />
- </menu>
- </item>
- <group android:id="@+id/group_delete">
- <item android:id="@+id/menu_archive"
- android:title="组员一" />
- <item android:id="@+id/menu_delete"
- android:title="组员 二" />
- </group>
- </menu>
很简单的实现,每天进步一点点,吼吼。。学习再学习!加油。。。