Android 长按弹出菜单选项:ContextMenu(长按弹出标记消息、消息置顶、删除消息)

效果如图:



package com.example.contextmenu;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.view.ContextMenu.ContextMenuInfo;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		TextView text = (TextView) findViewById(R.id.text);

		text.setOnCreateContextMenuListener(this);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

		menu.add(1, 1000, 0, "标记为未读");
		menu.add(1, 1001, 1, "消息置顶");
		menu.add(1, 1002, 2, "删除该消息");

		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case 1000:

			Toast.makeText(this, "标记为未读", Toast.LENGTH_SHORT).show();
			
			break;
		case 1001:

			Toast.makeText(this, "消息置顶", Toast.LENGTH_SHORT).show();
			break;
		case 1002:

			Toast.makeText(this, "删除该消息", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}

		boolean b = super.onContextItemSelected(item);
		Log.d("11111111111", b+"");

		return super.onContextItemSelected(item);
	}
}

你可能感兴趣的:(Android 长按弹出菜单选项:ContextMenu(长按弹出标记消息、消息置顶、删除消息))