Devices with Android + MIUI and setCustomSelectionActionModeCallback

前端 未结 4 1682
迷失自我
迷失自我 2021-02-04 23:54

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

4条回答
  •  日久生厌
    2021-02-05 00:20

    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:

    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

    Acivity:

    String html = "\n" +
            "\n" +
            "\n" +
            "\n" +
            "\n" +
            "\n" +
            "

    WebView text

    \n" + "\n" + "\n" + "\n"; @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); }

    Menu:

    
    
    
    
    
    
    
    

    Result:

提交回复
热议问题