search

Home  >  Q&A  >  body text

android - 如何修改WebView文本选中时的Contextual Action Bar为Floating Context Menu?

我想实现这种效果:

而选中WebView的文本时,默认是这样的效果:

跪求指点,弄好好几天了。

PHP中文网PHP中文网2772 days ago1031

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 14:33:23

    Update

    The initial idea: Try to listen to the WebView text selection event to pop up a custom context menu, failed!
    I found an open source code: BTAndroidWebViewSelection
    Someone packaged this source code into a library:
    Support library for text selection on Android WebView. (forked from BTAndroidWebViewSelection)

    I've Seen a lot of people trying to get user selections with a context menu working in Android web views. The problem lies in Android's use of an intermediate text view that it places between the user and the web view on selection. So the javascript in the page doesn't know what's selected.
    This solution uses a javascript interface to pass touches to the page and effectively cut Android's native selection out of the equation. This has been tested from 2.2 to 4.0 .3.

    This should be almost the effect you want:

    As for how to use it, just look at the source code


    The initial idea

    The context menu that pops up after long pressing in WebView is contextual action mode, which is the second picture you posted. It seems to be the default feature of WebView. The first picture you posted is floating context menu.
    Official document menus.html#context-menu

    If you want to change this default feature, the idea I can think of is:

    • Define a context menu and register it with your WebView: registerForContextMenu(View view);
      Override the two callback functions onCreateContextMenu() and onContextItemSelected() of the context menu to respond to the context menu click event;
      This is the standard step for implementing a floating context menu given in the official document;
      It should be noted that the context menu implemented by this method is a listview, which may need to be further customized to achieve the layout you want;

    • Then you need to listen to the WebView text selection event, so that after the text is selected, call Activity.openContextMenu() to pop up the custom floating context menu;
      webview does not provide a text selection listener, so you may have to You need to inherit WebView and override some of its methods.

    // Step 1
    // Registers a context menu to be shown for the given view.
    registerForContextMenu(mWebView);
    
    // Called when the context menu for this view is being built.
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.your_custom_context_menu, menu);
    }
    
    // Called whenever an item in a context menu is selected.
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // do your stuff to respond for menu item selected
        // ...
        return super.onContextItemSelected(item);
    }
    
    // Step 2
    // 可能需要继承webview以在选中文本后弹出context menu
            ...
            // Programmatically opens the context menu for a particular view.
            openContextMenu(v);
            ...
    });

    But now I am stuck on step 2. Even if the WebView is overwritten, the selection effect cannot be achieved. The options menu will always pop up after long pressing the WebView.

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 14:33:23

    LZ, why can’t I register the one? OnCreateContextMenu is a custom class that appears

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 14:33:23

    lz Please tell me, my needs are exactly opposite to yours, how can I block the system’s Floating Context Menu and change it to a Contextual Action Bar? I replaced the system's webview with Crosswalk. Under android 6.0 and below, it is a real Contextual Action Bar, but on android 6.0, it is displayed as Floating Context Menu. I would like to ask how to unify this?

    reply
    0
  • Cancelreply