Android EditText selectAll() doesn't works if one taps on the same field in android 4.x only

烂漫一生 提交于 2019-12-11 22:59:43

问题


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

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

}

回答1:


I solved this by just adding one property in layout.xml.

android:selectAllOnFocus="true"

Earlier i was not aware of this but this is really a very good solution for this kind of problems.




回答2:


try this...

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



回答3:


See this solution: https://stackoverflow.com/a/35527348/1504248

May be you need run something like:

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

inside your if(hasFocus).



来源:https://stackoverflow.com/questions/11793635/android-edittext-selectall-doesnt-works-if-one-taps-on-the-same-field-in-andr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!