首先我们要重写上下文菜单方法onCreateContextMenu,从这个方法可以添加需要的条目按钮,我们要在res/menu目录下建议个weixin.xml文件:
再重写onContextItemSelected,在这个里面实现自己需要点击按钮后需要实现的逻辑业务。
java代码如下:
package com.xh.tx.menue;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private ArrayList date=null;
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//TextView tx=(TextView) findViewById(R.id.text);
//tx.setOnCreateContextMenuListener(this);
date=new ArrayList();
for(int i=0;i<50;i++){
date.add("测试数据"+i);
}
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, date);
setListAdapter(adapter);
ListView lv=getListView();
lv.setOnCreateContextMenuListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_settings1:
Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
break;
case R.id.action_settings2:
Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
break;
case R.id.action_settings3:
Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
break;
case R.id.action_settings4:
Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.weixin, menu);
// menu.add(1, 101, 0, "未读");
// menu.add(1, 102, 1, "置顶");
// menu.add(1, 103, 2, "删除");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info=(AdapterContextMenuInfo) item.getMenuInfo();
final int pos=info.position;
int id=item.getItemId();
switch (id) {
case R.id.weidu:
Toast.makeText(this, item.getItemId()+"未读"+R.id.weidu, Toast.LENGTH_LONG).show();
break;
case R.id.zhiding:
AlertDialog dialog=new AlertDialog.Builder(this).create();
dialog.setTitle("对话框");
dialog.setMessage("是否置顶");
dialog.setButton(AlertDialog.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String s=date.remove(pos);
date.add(0, s);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "置顶成功", Toast.LENGTH_LONG).show();
}
});
dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "取消置顶", Toast.LENGTH_LONG).show();
}
});
dialog.show();
break;
case R.id.shanchu:
AlertDialog dialog1=new AlertDialog.Builder(this).create();
dialog1.setTitle("对话框");
dialog1.setMessage("是否确认删除内容");
dialog1.setButton(AlertDialog.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
date.remove(pos);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_LONG).show();
}
});
dialog1.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "取消删除", Toast.LENGTH_LONG).show();
}
});
dialog1.show();
break;
}
return super.onContextItemSelected(item);
}
}
运行结果如下图: