提问者:小点点

Android:如何完全禁用编辑文本中的复制和粘贴功能


我是Android开发领域的新手,最近我遇到了一个棘手的问题。

我试图制作一个不应允许用户从中复制内容或粘贴内容的编辑文本。我在谷歌上搜索了很多,发现似乎有两种流行的方法:

第一种方法,在布局文件中设置它:

android:longClickable="false"

第二种方法,以编程方式设置它:

myEdittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        public void onDestroyActionMode(ActionMode mode) {

        }

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            return false;
        }

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

但是我刚刚发现,无论我选择哪种方式,编辑文本区域只能从长点击中禁用,这会阻止用户通过长点击访问“全选、复制和粘贴”菜单。但是2解决方案都DID阻止用户通过简单的点击光标来访问“粘贴”功能。

所以我的问题是:我如何完全阻止用户在某个编辑文本中复制和粘贴功能。有人帮忙吗?谢谢你


共3个答案

匿名用户

您可以完全隐藏“全选、复制和粘贴”菜单以及简单点击光标时弹出的“粘贴”功能。

为此,您必须创建一个自定义EditText类。这是一个例子…

// Custom EditText class
public class NoMenuEditText extends EditText
{
    private final Context context;

    /** This is a replacement method for the base TextView class' method of the same name. This 
    * method is used in hidden class android.widget.Editor to determine whether the PASTE/REPLACE popup
    * appears when triggered from the text insertion handle. Returning false forces this window
    * to never appear.
    * @return false
    */
    boolean canPaste()
    {
        return false;
    }

    /** This is a replacement method for the base TextView class' method of the same name. This method
    * is used in hidden class android.widget.Editor to determine whether the PASTE/REPLACE popup
    * appears when triggered from the text insertion handle. Returning false forces this window
    * to never appear.
    * @return false
    */
    @Override
    public boolean isSuggestionsEnabled()
    {
        return false;
    }

    public NoMenuEditText(Context context)
    {
        super(context);
        this.context = context;
        init();
    }

    public NoMenuEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.context = context;
        init();
    }

    public NoMenuEditText(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        this.context = context;
        init();
    }

    private void init()
    {
        this.setCustomSelectionActionModeCallback(new ActionModeCallbackInterceptor());
        this.setLongClickable(false);
    }


    /**
    * Prevents the action bar (top horizontal bar with cut, copy, paste, etc.) from appearing
    * by intercepting the callback that would cause it to be created, and returning false.
    */
    private class ActionModeCallbackInterceptor implements ActionMode.Callback
    {
        private final String TAG = NoMenuEditText.class.getSimpleName();

        public boolean onCreateActionMode(ActionMode mode, Menu menu) { return false; }
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; }
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; }
        public void onDestroyActionMode(ActionMode mode) {}
    }
} 

在您的布局中使用此EditText。现在,它不会显示任何复制/粘贴菜单。它将仅显示蓝色句柄,但当您单击时,您将不会弹出任何粘贴选项。

希望这有助于…

匿名用户

最佳编程方式是:

myEdittext.setLongClickable(false);

或者,只是在xml中

android:longClickable="false"

匿名用户

解决办法很简单

public class MainActivity extends AppCompatActivity {

EditText et_0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et_0 = findViewById(R.id.et_0);

    et_0.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            //to keep the text selection capability available ( selection cursor)
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            //to prevent the menu from appearing
            menu.clear();
            return false;
        }

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

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });
   }
}

样本预览