具有 Android + MIUI 和 setCustomSelectionActionModeCallback 的设备

编程入门 行业动态 更新时间:2024-10-08 08:34:03
本文介绍了具有 Android + MIUI 和 setCustomSelectionActionModeCallback 的设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建自定义选择菜单,但它不适用于带有 rom MIUI 和 Android 6 的设备.结果是带有 copy 和 select 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 的设备

本文发布于:2023-11-25 21:00:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设备   Android   MIUI   setCustomSelectionActionModeCallback

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!