码迷,mamicode.com
首页 > 移动开发 > 详细

Android ContextMenu的使用

时间:2017-07-04 14:45:50      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:分享   推断   emc   text   art   actionbar   protect   todo   ret   

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的使用

标签:分享   推断   emc   text   art   actionbar   protect   todo   ret   

原文地址:http://www.cnblogs.com/cynchanpin/p/7116068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!