4

在搜索了一个类似的问题后,我放弃了,决定问自己,如果有现成的答案,请通知我。

我有一个 WebView,我需要在其中覆盖默认的 actionMode ,我在这里遵循了许多问答的建议,一切都运行良好,直到我注意到 actionMode 的行为中缺少一些非常具体的东西。文本选择的关闭事件仅在用户在操作模式下按下“后退按钮”或“完成”时发生。在默认操作模式中,当用户在所选文本之外按下操作模式时,操作模式也会关闭,我无法实现这一点,也不知道如何在不使事情复杂化和编写额外代码行的情况下实现它,而不是使用“超级”行为来解除文本当用户在所选文本之外按下时选择。

希望你能帮忙,先谢谢了。

我的 WebView 具有覆盖的 startActionMode:

@Override
public ActionMode startActionMode(ActionMode.Callback callback) {

    /*
     * When running Ice Cream Sandwich (4.0) or Jelly Bean (4.1 - 4.3),
     * there is a hidden class called 'WebViewClassic' that draws the
     * selection. In order to clear the selection, save the callback from
     * Classic so it can be destroyed later.
     */
    // Check the class name because WebViewClassic.SelectActionModeCallback
    // is not public API.
    String name = callback.getClass().toString();
    if (name.contains("SelectActionModeCallback")) {
        webViewDefaultActionModeCallBack = callback;
    }

    if (mActionMode == null) {
        mActionMode = super.startActionMode(mActionModeCallback);
    }
    return mActionMode;
}

我的 ActionModeCallback :

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    // Called when the action mode is created; startActionMode() was called
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {

        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.my_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) {

        return false; // Return false if nothing is done
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {

            case R.id.item1:
                // do something 
                break;

            case R.id.item2:
                // do something 
                break;
        }

        mode.finish();
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {

        mActionMode = null;

        // Semi-hack in order to clear the selection
        // when running Android earlier than KitKat.
        if (webViewDefaultActionModeCallBack != null) {
            webViewDefaultActionModeCallBack.onDestroyActionMode(mode);
        }
    }
};
4

0 回答 0