ActionMode上下文操作模式
2023-01-24 09:16:15 最后更新
Toolbar是个好东西,但是一旦需要动态添加减少菜单,那可就不好办(我看你能不能动态添加减少,我试了一下,4个菜单项用了我31行),于是,ActionMode趁机而生。
它是啥自行百度,我可以跟你说iApp代码编辑框的长按顶上那个东东就是它
实现也挺简单,不过本应该用javacb的用不了(其实理应当用得了),对于iApp的javacb我真的有话要说,遇到抽象方法要返回值的如果boolean他就返回false,导致系统看见false就不创建了
(这也是为什么你用javacb设置事件有时闪退,因为应该返回值true的)
如果你要多个的话最好多弄几个
AndroidActionMode全部源码我会发在评论区。

建个util.mjava

import android.view.ActionMode;
import android.view.View;

//参数tbr是为了防止ActionMode把Toolbar顶下去(因为不会重叠),如果没有toolbar的话那就不用了
public static void startActionMode1(View tbr)
{
 activity.startActionMode(new ActionMode.Callback()
 {
  //不用@Override,因为会报错
  /**
   * Called when action mode is first created. The menu supplied will be used to
   * generate action buttons for the action mode.
   * 在首次创建操作模式时调用。提供的菜单将用于为操作模式生成操作按钮。
   * @param mode ActionMode being created
   * @param menu Menu used to populate action buttons
   * @return true if the action mode should be created, false if entering this
   *              mode should be aborted.
   */
   public boolean onCreateActionMode(ActionMode mode, Menu menu)
   {
    //TODO: Important this method
    //把Toolbar设置隐藏
    //设置标题
    p1.setTitle("标题");
    //设置副标题
    p1.setSubtitle("副标题");
    //设置布局
    p1.setCustomView(new android.widget.EditText(activity));
    //设置类型(不知道有啥用)
    p1.setType(ActionMode.TYPE_FLOATING);
    //ActionMode.TYPE_PRIMARY
    i.us(tbr,"visibility","gone");
    return true;
   }
        /**
         * Called to refresh an action mode's action menu whenever it is invalidated.
         * 调用以在操作模式的操作菜单失效时刷新该菜单。
         * @param mode ActionMode being prepared
         * @param menu Menu used to populate action buttons
         * @return true if the menu or action mode was updated, false otherwise.
         */
   public boolean onPrepareActionMode(ActionMode mode, Menu menu)
   {
//TODO: Important this method
    return true;
   }
 /**
         * Called to report a user click on an action button.
         * 调用以报告用户单击操作按钮。
         * @param mode The current ActionMode
         * @param item The item that was clicked
         * @return true if this callback handled the event, false if the standard MenuItem
         *          invocation should continue.
         */
   public boolean onActionItemClicked(ActionMode mode, MenuItem item)
   {
    //TODO: Important this method
    return false;
   }
      /**
         * Called when an action mode is about to be exited and destroyed.
         * 当操作模式即将退出和销毁时调用。
         * @param mode The current ActionMode being destroyed
         */
   public void onDestroyActionMode(ActionMode mode)
   {
    //TODO: Important this method
    i.us(tbr,"visibility","visible");
   }
 });
}
调用:
gvs(Toolbarid,tbr)
call(null,"mjava","util.startActionMode1",tbr)

.作者:MC的wither菌