Android:如何在Edittext中完全禁用复制和粘贴功能

时间:2014-10-03 07:39:50

标签: android android-edittext copy-paste

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

我试图创建一个Edittext,它不应该允许用户从中复制内容或将内容粘贴到其中。我搜索了很多,发现似乎有两种流行的方式:

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

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;
        }
    });

但我发现无论我选择哪种方式,只能通过长按来禁用edittext区域,这会阻止用户通过长按来访问“全选,复制和粘贴”菜单。但是这两个解决方案都不能阻止用户通过简单地点击光标来访问“粘贴”功能。

所以我的问题是:我怎么能完全阻止用户在某个Edittext中复制和粘贴功能。有人帮忙吗?很多

4 个答案:

答案 0 :(得分:5)

你可以完全隐藏"全选,复制和粘贴"菜单以及"粘贴"单击光标时弹出的功能。

为此你必须创建一个自定义的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。现在,它不会显示任何复制/粘贴菜单。它只显示蓝色手柄,但是当你点击它时,你不会弹出任何粘贴选项。

希望这会有所帮助......

答案 1 :(得分:5)

最好的编程方式是:

myEdittext.setLongClickable(false);

或者,只是在xml中

android:longClickable="false"

答案 2 :(得分:1)

通过禁用游标处理程序,有一种可能性。您不会获得粘贴按钮,但也无法通过触摸移动光标。

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getActionMasked() == MotionEvent.ACTION_UP && mDisableCursorHandle) {
        // Hack to prevent keyboard and insertion handle from showing.
        cancelLongPress();
    }
    return super.onTouchEvent(event);
}

答案 3 :(得分:0)

解决方案非常简单

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) {

        }
    });
   }
}

sample preview