Android EditText selectAll () does not work if one click on the same field only in android 4.x

I want to select all the text present in the EditText field, if one clicks on the same one, right now I can achieve this by specifying the code below in android 2.x and 3.x, but not in 4.0. If anyone had the same problem, please let me know how to solve this problem.

@Override
public void onFocusChange(View v, boolean hasFocus) {
    switch(v.getId()) {
    case R.id.passcodetext1:
        if(hasFocus) editText1.selectAll();
        break;

    case R.id.passcodetext2:
        if(hasFocus) editText2.selectAll();
        break;

    case R.id.passcodetext3:
        if(hasFocus) editText3.selectAll();
        break;

    case R.id.passcodetext4:
        if(hasFocus) editText4.selectAll();
        break;
    }

}
0
source share
3 answers

I solved this by simply adding one property to layout.xml.

android:selectAllOnFocus="true"

I did not know about this before, but it is really a very good solution for such problems.

+3
source

try it...

 if(hasFocus) v.selectAll();
              ^^^^^^^^^^^
0
source

: fooobar.com/questions/1535521/...

, - :

        Editable text = edit.getText();
        if (text.length() > 0) {
            text.replace(0, 1, text.subSequence(0, 1), 0, 1);
            edit.selectAll();
        }

if(hasFocus).

0
source

Source: https://habr.com/ru/post/1535523/


All Articles