具有 Android + MIUI 和 setCustomSelectionActionModeCallback 的设备 [英] Devices with Android + MIUI and setCustomSelectionActionModeCallback

查看:20
本文介绍了具有 Android + MIUI 和 setCustomSelectionActionModeCallback 的设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建自定义选择菜单,但它不适用于带有 rom MIUI 和 Android 6 的设备.结果是带有 copyselect all 项目.在干净的 Android 下的其他设备和模拟器上它工作得很好.

I'm trying to create custom selection menu but it does not work on a device with rom MIUI and Android 6. The result is common menu with copy and select all items. On other devices and simulators under clean Android it works just fine.

代码:

        textViewTop.setCustomSelectionActionModeCallback(new android.view.ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {

            Log.d(LOG_TAG, "onCreateActionMode");

            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            Log.d(LOG_TAG, "onPrepareActionMode");
            menu.clear();

            int quote_quick = R.drawable.ic_desktop_mac_black_24dp;
            int quote_add = R.drawable.ic_computer_black_24dp;
            int copy = R.drawable.ic_devices_other_black_24dp;

            menu.add(Menu.NONE, QUOTE_START, 3, "").setIcon(quote_quick).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
            menu.add(Menu.NONE, QUOTE_ADD, 2, "").setIcon(quote_add).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
            menu.add(Menu.NONE, CUSTOM_COPY, 1, "").setIcon(copy).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });

推荐答案

所以我想出了一个解决方法,但只有当你绝对需要它在 MIUI 设备上工作时才有意义.这通常有点尴尬:

So I figured out a workaround, but it makes sense only if you absolutely need it to work on MIUI devices. It's generally a little awkward:

我注意到 Wikipedia 应用程序具有在小米设备上运行的自定义操作,在查看代码后我发现在 WebView 中选择文本时它可以正常工作.您基本上可以在 Activity

I noticed that the Wikipedia app has custom actions working on a Xiaomi device, and after looking through the code I found out it works fine when the texts is selected in a WebView. You can basically use a WebView and override onActionModeStarted in your Activity

活动:

String html = "<!DOCTYPE html>
" +
        "<html>
" +
        "<head>
" +
        "</head>
" +
        "<body>
" +
        "
" +
        "<h1>WebView text</h1>
" +
        "
" +
        "</body>
" +
        "</html>
";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView webView = findViewById(R.id.web_view);
    webView.setWebViewClient(new WebViewClient());
    webView.loadData(html, "text/html", "UTF-8");
}

@Override
public void onActionModeStarted(ActionMode mode) {
    super.onActionModeStarted(mode);
        Menu menu = mode.getMenu();
        menu.clear();
        mode.getMenuInflater().inflate(R.menu.menu_text_select, menu);
}

菜单:

<item android:id="@+id/id1"
      android:title="miui"
      app:showAsAction="ifRoom" />

<item android:id="@+id/id2"
      android:title="has"
      app:showAsAction="ifRoom" />

<item android:id="@+id/id3"
      android:title="bugs"
      app:showAsAction="ifRoom" />

<item android:id="@+id/id4"
    android:title="D:"
    app:showAsAction="ifRoom" />

结果:

这篇关于具有 Android + MIUI 和 setCustomSelectionActionModeCallback 的设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆