How to disable copy/paste from/to EditText

后端 未结 24 1312
情歌与酒
情歌与酒 2020-11-22 12:18

In my application, there is a registration screen, where i do not want the user to be able to copy/paste text into the EditText field. I have set an onLon

相关标签:
24条回答
  • 2020-11-22 12:47

    I found that when you create an input filter to avoid entry of unwanted characters, pasting such characters into the edit text is having no effect. So this sort of solves my problem as well.

    0 讨论(0)
  • 2020-11-22 12:48

    You can do this by disabling the long press of the EditText

    To implement it, just add the following line in the xml -

    android:longClickable="false"
    
    0 讨论(0)
  • 2020-11-22 12:48

    https://github.com/neopixl/PixlUI provides an EditText with a method

    myEditText.disableCopyAndPaste().

    And it's works on the old API

    0 讨论(0)
  • 2020-11-22 12:52

    the solution is very simple

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

    --------> preview <---------

    0 讨论(0)
  • 2020-11-22 12:52

    Solution that worked for me was to create custom Edittext and override following method:

    public class MyEditText extends EditText {
    
    private int mPreviousCursorPosition;
    
    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        CharSequence text = getText();
        if (text != null) {
            if (selStart != selEnd) {
                setSelection(mPreviousCursorPosition, mPreviousCursorPosition);
                return;
            }
        }
        mPreviousCursorPosition = selStart;
        super.onSelectionChanged(selStart, selEnd);
    }
    

    }

    0 讨论(0)
  • 2020-11-22 12:53

    You may try android:focusableInTouchMode="false".

    0 讨论(0)
提交回复
热议问题