菜单列表的增加,删除,修改等两种方法
第一种方法内容在menu文件夹中自定义XML
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/add" android:title="增加"/> <item android:id="@+id/delete" android:title="删除"/> <item android:id="@+id/update" android:title="修改"/> </menu>
public class MainActivity extends Activity { private TextView textview; private LinearLayout lin; private EditText edittext; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview = (TextView) findViewById(R.id.textview); lin = (LinearLayout) findViewById(R.id.linlayout); edittext = (EditText) findViewById(R.id.edittext); button = (Button) findViewById(R.id.button); // 注册上下文菜单 MainActivity.this.registerForContextMenu(textview); } // 生成上下文菜单 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // 得到XML文件下的菜单界面 getMenuInflater().inflate(R.menu.item_menu, menu); } // 上下文菜单项被选中 @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.add: Toast.makeText(MainActivity.this, "您选择了增加", 0).show(); break; case R.id.delete: Toast.makeText(MainActivity.this, "您选择了删除", 0).show(); break; case R.id.update: Toast.makeText(MainActivity.this, "您选择了修改", 0).show(); lin.setVisibility(View.VISIBLE); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 判断输入框是否为空 if (!TextUtils.isEmpty(edittext.getText().toString())) { // 修改原始文本的内容 textview.setText(edittext.getText().toString()); // 把输入框设置为空 edittext.setText(""); // 隐藏LinearLayout界面 lin.setVisibility(View.GONE); } } }); default: break; } return super.onContextItemSelected(item); }
第二种方法在代码中添加内容
public class MainActivity extends Activity { private ListView listview; private ArrayList<String> list; private EditText name; private LinearLayout lin; private Button tijiao; private int m; private Base base; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.listview); lin = (LinearLayout) findViewById(R.id.lin); name = (EditText) findViewById(R.id.edittext); tijiao = (Button) findViewById(R.id.tijiao); list = new ArrayList<String>(); for (int i = 0; i < 100; i++) { list.add("这是list" + i); } base = new Base(list, MainActivity.this); listview.setAdapter(base); // 注册上下文 MainActivity.this.registerForContextMenu(listview); } // 生成上下文菜单 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, 0, ContextMenu.NONE, "增加"); menu.add(0, 1, ContextMenu.NONE, "删除"); menu.add(0, 2, ContextMenu.NONE, "修改"); } // 设置上下文点击事件 @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); m = info.position; switch (item.getItemId()) { case 0://增加 lin.setVisibility(View.VISIBLE); tijiao.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (!TextUtils.isEmpty(name.getText().toString())) { list.add(m, name.getText().toString()); base.notifyDataSetChanged(); name.setText(""); lin.setVisibility(View.GONE); } } }); break; case 1://删除 list.remove(m); base.notifyDataSetChanged(); break; case 2://修改 lin.setVisibility(View.VISIBLE); tijiao.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (!TextUtils.isEmpty(name.getText().toString())) { list.remove(m); list.add(m, name.getText().toString()); base.notifyDataSetChanged(); name.setText(""); lin.setVisibility(View.GONE); } } }); break; default: break; } return super.onContextItemSelected(item); }