How to empty edittext without setText(“”);

后端 未结 5 1607
青春惊慌失措
青春惊慌失措 2021-01-07 20:24

Is there a way to reset the edittext value without setting text like:

((EditText) findViewById(R.id.yoursXmlId)).setText(\"\");

EDI

相关标签:
5条回答
  • 2021-01-07 20:40

    I had a case where a Samsung device was not blanking out text with setText(""), so I used:

    String BLANK = "\u0020"; // space character

    setText (BLANK);

    and it worked this way. It may have been a font issue.

    0 讨论(0)
  • 2021-01-07 20:41

    if anyone is finding the selected answer not working please try with

    editText.post(() -> editText.getText().clear());
    

    or

    editText.post(new Runnable() {
                @Override
                public void run() {
                    editText.getText().clear();
                }
            });
    
    0 讨论(0)
  • 2021-01-07 20:47

    Another option is: EditText.getText().clear(); But you'll have to cast anyway:

    ((EditText) findViewById(R.id.yoursXmlId)).getText().clear();

    0 讨论(0)
  • 2021-01-07 20:50

    you can use following way

    Kotlin:

    myEditText.text.clear()
    

    Java:

    EditText myEditText = ((EditText) findViewById(R.id.yoursXmlId));
    myEditText.getText().clear()
    
    0 讨论(0)
  • 2021-01-07 20:52

    A function for that would be:

    fun setTextViewEmpty(textView: TextView){
    (textView as EditText ).text.clear()   }
    

    inspired by this

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