问题
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