【问题标题】:How to disable sharing from app selected text如何从应用选择的文本中禁用共享
【发布时间】:2018-07-16 10:36:29
【问题描述】:

我的 Android 应用中有一个 WebView 和一些 EditTexts。当我选择文本时,我可以选择共享此文本。 现在我想在我的整个应用程序中禁用此文本共享功能。 最可靠的方法是什么?

【问题讨论】:

标签: android webview android-edittext android-sharing


【解决方案1】:

对于网络视图,有一个很好的方法[setDisabledActionModeMenuItems][1],但它仅适用于 android sdk 24+。

我是这样使用反射的

  private void disableSelectedTextOptions() {
    // TODO Method setDisabledActionModeMenuItems should be called directly after project will be migrated to SDK 24+
    if (Build.VERSION.SDK_INT < 24) return;
    Method method = null;
    try {
      method = webSettings.getClass().getMethod("setDisabledActionModeMenuItems", int.class);
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
      return;
    }
    try {
      method.invoke(webSettings,
          /*WebSettings.MENU_ITEM_SHARE*/ 1 |
          /*WebSettings.MENU_ITEM_WEB_SEARCH*/ 2 |
          /*WebSettings.MENU_ITEM_PROCESS_TEXT*/ 4);
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
  }

对于和 EditText 我使用这种方法来删除“共享”(和“翻译”):

public static void removeShareOption(TextView tv){
    tv.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            return true;
        }

        @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            menu.removeItem(/*android.R.id.shareText*/0x01020035);
            menu.removeItem(/* Translate */0);
            return true;
        }

        @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }

        @Override public void onDestroyActionMode(ActionMode mode) {

        }
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 2011-02-11
    相关资源
    最近更新 更多