上下文菜单,当用户长时间点击的时候,出现
第一步:注册绑定按钮
registerForContextMenu(findViewById(R.id.ctx_btn));
第二步:设置监听器
findViewById(R.id.ctx_btn).setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { startActionMode(cb); return false; } });
第三步:对监听器设置事件
ActionMode.Callback cb = new ActionMode.Callback() { //创建,在启动上下文模式操作(startActionMode(Callback))时调用 @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { Log.e("TAG", "创建"); getMenuInflater().inflate(R.menu.context, menu); return true; } //在创建方法后进行调用 @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { Log.e("TAG", "准备"); return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { Log.e("TAG", "点击"); switch (item.getItemId()) { case R.id.delete: Toast.makeText(MainActivity.this, "删除", Toast.LENGTH_SHORT).show(); break; case R.id.opera1: Toast.makeText(MainActivity.this, "操作1", Toast.LENGTH_SHORT).show(); break; case R.id.opera2: Toast.makeText(MainActivity.this, "操作2", Toast.LENGTH_SHORT).show(); break; } return true; } //上下文操作模式结束时被调用 @Override public void onDestroyActionMode(ActionMode mode) { Log.e("TAG", "结束"); } };
Java完整代码
package com.example.imoocapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.ActionMode; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //ctx_btn: 演示ContextMenu //1.注册 registerForContextMenu(findViewById(R.id.ctx_btn)); //2.创建 覆盖 onCreateContextMenu //3.菜单项的操作 onContextItemSelected //4.为按钮设置上下文操作模式 //1实现ActionMode CallBack //2在View的长按事件中去启动上下文操作模式 findViewById(R.id.ctx_btn).setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { startActionMode(cb); return false; } }); } /* @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.delete: Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show(); break; case R.id.opera1: Toast.makeText(this, "操作1", Toast.LENGTH_SHORT).show(); break; case R.id.opera2: Toast.makeText(this, "操作2", Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { getMenuInflater().inflate(R.menu.context, menu); } */ ActionMode.Callback cb = new ActionMode.Callback() { //创建,在启动上下文模式操作(startActionMode(Callback))时调用 @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { Log.e("TAG", "创建"); getMenuInflater().inflate(R.menu.context, menu); return true; } //在创建方法后进行调用 @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { Log.e("TAG", "准备"); return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { Log.e("TAG", "点击"); switch (item.getItemId()) { case R.id.delete: Toast.makeText(MainActivity.this, "删除", Toast.LENGTH_SHORT).show(); break; case R.id.opera1: Toast.makeText(MainActivity.this, "操作1", Toast.LENGTH_SHORT).show(); break; case R.id.opera2: Toast.makeText(MainActivity.this, "操作2", Toast.LENGTH_SHORT).show(); break; } return true; } //上下文操作模式结束时被调用 @Override public void onDestroyActionMode(ActionMode mode) { Log.e("TAG", "结束"); } }; }
content.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/delete" android:title="删除" /> <item android:title="重命名" > <menu > <item android:id="@+id/opera1" android:title="操作1" /> <item android:id="@+id/opera2" android:title="操作2" /> </menu> </item> </menu>
activity.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/ctx_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上下文菜单" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>