首页 > 代码库 > Android ContextMenu的使用

Android ContextMenu的使用

ContextMenu介绍:

假设一个View注冊了上下文菜单。那么当长按该View时便会弹出一个浮动菜单,来供选择下一步操作。

技术分享

实现这个功能须要调用setOnCreateContextMenuListener来注冊一个监听器。那么它和注冊长按监听器setOnLongClickListener有什么差别呢?以下会在代码中加以測试,通过加入打印发现,用户对某个View长按之后。首先触发的是onLongClick函数,运行onLongClick操作,然后依据onLongClick的返回值推断是不是运行OnCreateContextMenuListener函数。假设onLongClick返回false。则会继续运行OnCreateContextMenuListener函数,假设返回true。忽略OnCreateContextMenuListener函数。

代码例如以下:

public class MainActivity extends ActionBarActivity {

	ImageView mImageView ;
	MyContextMenuClickListener mContextListener;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);

		mImageView = (ImageView) findViewById(R.id.image);
		mContextListener = new MyContextMenuClickListener();
		mImageView.setOnCreateContextMenuListener(mContextListener);
		
		//test
		mImageView.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View arg0) {
				// TODO Auto-generated method stub
				Log.d("Test", "onLongClick");
				return false;
			}
		});
	}

	private class MyContextMenuClickListener implements OnCreateContextMenuListener , OnMenuItemClickListener{

		@Override
		public boolean onMenuItemClick(MenuItem item) {
			// TODO Auto-generated method stub
			switch(item.getItemId()){
			case R.id.open:
				Log.d("Test", "click open");
				break;
			case R.id.delete:
				Log.d("Test", "click delete");
				break;
			case R.id.remove:
				Log.d("Test", "click remove");
				break;
			default:
					break;
			}
			return true;
		}

		@Override
		public void onCreateContextMenu(ContextMenu menu, View v,
				ContextMenuInfo menuInfo) {
			// TODO Auto-generated method stub
			Log.d("Test", "onCreateContextMenu");
			
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.context_menu, menu);
			LayoutInflater layoutInflater = getLayoutInflater();
			View view = (View) layoutInflater.inflate(
					R.layout.menu_header, null);
			menu.setHeaderView(view);
			
			MenuItem open = menu.findItem(R.id.open);
			open.setOnMenuItemClickListener(mContextListener);
			MenuItem delete = menu.findItem(R.id.delete);
			delete.setOnMenuItemClickListener(mContextListener);
			MenuItem remove = menu.findItem(R.id.remove);
			remove.setOnMenuItemClickListener(mContextListener);
		}
		
	}

}

context_menu.xml

<?xml version="1.0" encoding="UTF-8"?

> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:id="@+id/CONTEXT_MENU"> <item android:id="@+id/delete" android:title="delete"/> <item android:id="@+id/open" android:title="open"/> <item android:id="@+id/remove" android:title="remove"/> </group> </menu>


menu_header.xml

<?

xml version="1.0" encoding="UTF-8"?

> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a Test" /> </LinearLayout>


假设须要自己关闭ContextMenu弹框。能够调用:

getActivity().closeContextMenu();




Android ContextMenu的使用