【问题标题】:Selection Handles Not Appearing in WebView (Android 4.0-4.3)选择句柄未出现在 WebView (Android 4.0-4.3)
【发布时间】:2016-02-19 13:23:06
【问题描述】:

我创建了一个自定义ActionMode.Callback,以便在WebView 中显示自定义上下文操作栏以进行文本选择。它在 Android 4.4 中运行良好,但在 4.1-4.3 中,选择句柄没有出现。我仍然可以对突出显示的单个单词执行自定义操作,但由于缺少句柄,无法更改选择。

此外,当ActionMode 被销毁时,选择不会从屏幕上清除。相反,如果用户点击其他地方来清除选择,ActionMode 不会被破坏。

我什至不确定clearFocus() 是我应该调用的方法来尝试删除选择。我评论了那条线,4.4 中的行为没有改变;它仍然完美无缺。

可以做些什么来解决这些问题?


这是我目前的实现:
public class CustomWebView extends WebView {

    private ActionMode.Callback mActionModeCallback;

    @Override
    public ActionMode startActionMode(Callback callback) {
        ViewParent parent = getParent();
        if (parent == null) {
            return null;
        }
        mActionModeCallback = new CustomActionModeCallback();
        return parent.startActionModeForChild(this, mActionModeCallback);
    }

    private class CustomActionModeCallback implements ActionMode.Callback {

        // Called when the action mode is created; startActionMode() was called
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.contextual_menu, menu);
            return true;
        }

        // Called each time the action mode is shown.
        // Always called after onCreateActionMode, but
        // may be called multiple times if the mode is invalidated.
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // This method is called when the handlebars are moved.
            loadJavascript("javascript:getSelectedTextInfo()");
            return false; // Return false if nothing is done
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch(item.getItemId() {
            case R.id.button_1:
                // do stuff
                break;
            case R.id.button_2:
                // do stuff
                break;

            ... // cases for other buttons

            default:
                break;
            }

            mode.finish(); // Action picked, so close the CAB
            return true;
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO This does not work in 4.3 (and probably anything older).
            clearFocus(); // Remove the selection highlight and handles.

        }
    }
}

【问题讨论】:

    标签: android webview selection contextual-action-bar android-actionmode


    【解决方案1】:

    我从来没有弄清楚我做错了什么导致了这种行为。幸运的是,我确实找到了一种解决方案,可以适当地显示车把,并允许自定义操作。该解决方案可以在这个问题上找到:https://stackoverflow.com/a/22391169/2608235

    简而言之,与其创建自己的ActionMode,不如删除默认选择操作(复制、粘贴等),并使用MenuInflater 替换为您自己的menu.xml 布局,覆盖onActionModeStarted。为您的菜单元素定义一个onClick,在您的Activity 中实现该方法,就完成了。

    【讨论】:

      猜你喜欢
      • 2019-09-25
      • 2019-09-25
      • 2021-04-20
      • 2013-12-13
      • 1970-01-01
      • 2013-06-26
      • 2013-08-10
      • 1970-01-01
      • 2014-05-17
      相关资源
      最近更新 更多