package com.example.actionmode; import android.app.Activity; import android.os.Bundle; import android.view.ActionMode; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.widget.Button; import android.widget.PopupMenu; import android.widget.PopupMenu.OnMenuItemClickListener; import android.widget.Toast; public class MainActivity extends Activity { private Button button1; private Button button2; private ActionMode actionMode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { // TODO Auto-generated method stub if (actionMode != null) { return false; } actionMode = startActionMode(actionCallback); v.setSelected(true); return true; } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub PopupMenu popupMenu = new PopupMenu(MainActivity.this, v); popupMenu .setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.edit: edit(); break; case R.id.share: share(); break; case R.id.delete: delete(); break; default: break; } return false; } }); MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.main, popupMenu.getMenu()); popupMenu.show(); // 如果有空间,显示在view的上面,没空间显示在view的下面 } }); } private ActionMode.Callback actionCallback = new ActionMode.Callback() { @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { // TODO Auto-generated method stub return false; } @Override public void onDestroyActionMode(ActionMode mode) { // TODO Auto-generated method stub actionMode = null; } // 从xml加载菜单 @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { // TODO Auto-generated method stub MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.main, menu); return true; // 注意这里要返回true } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.edit: edit(); break; case R.id.share: share(); break; case R.id.delete: delete(); break; default: break; } return false; } }; public void edit() { Toast.makeText(MainActivity.this, "编辑", Toast.LENGTH_SHORT).show(); } public void share() { Toast.makeText(MainActivity.this, "分享", Toast.LENGTH_SHORT).show(); } public void delete() { Toast.makeText(MainActivity.this, "删除", Toast.LENGTH_SHORT).show(); } }
res/menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/edit" android:orderInCategory="100" android:showAsAction="never" android:title="编辑"/> <item android:id="@+id/share" android:orderInCategory="100" android:showAsAction="never" android:title="分享"/> <item android:id="@+id/delete" android:orderInCategory="100" android:showAsAction="never" android:title="删除"/> </menu>
时间: 2024-10-17 19:23:01