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

Android ContextMenu的使用

时间:2014-06-15 18:30:31      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:android   上下文菜单   contextmenu   

ContextMenu介绍:

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

bubuko.com,布布扣

实现这个功能需要调用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>


Android ContextMenu的使用,布布扣,bubuko.com

Android ContextMenu的使用

标签:android   上下文菜单   contextmenu   

原文地址:http://blog.csdn.net/heqiangflytosky/article/details/30480895

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