Android: select all text on focus on EditField

2020-07-21 06:16发布

问题:

I'm trying to get Android to select all the text in an EditText field when it gets the focus. I'm using this attribute in the layout (on both fields):

android:selectAllOnFocus="true"

I'm not sure if this is relevant, but to get the cursor to the first editable field (there's also a disabled field before it), I'm using the following commands:

quizStartNum.setFocusable(true);
quizStartNum.requestFocus();

But, while the cursor does move to the desired field when the layout is first displayed, the text doesn't get highlighted; instead the cursor ends up to the left of the text, the default behavior. If I move to the second field by touching it, all the text is selected as desired. Then, if I move back to the first field, again by touching it, the text is also completely selected. I would like to have the behavior right from the start. Is there a way to do this?

回答1:

If android:selectAllOnFocus="true" does not work, try calling setSelectAllOnFocus(true) on that particular EditText.

If that doesn't work either, this is another workaround from a previous SO post.

EditText dummy = ... 

dummy.setOnFocusChangedListener(new OnFocusChangedListener(){
    public void onFocusChange(View v, boolean hasFocus){
        if (hasFocus) && (isDummyText())
            ((EditText)v).selectAll();
    }
});


回答2:

I had a similar issue and android:selectAllOnFocus="true" did not work for me. The reason was that i was programatically requesting focus to the EditText before it was displayed. So if you are doing this for a EditText in an AlertDialog make sure you show it before you request focus to the EditText.



回答3:

EditText should be focussed after it displayed.

Following workout worked for me,

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Set Input Methode
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}

@Override
public void onStart() {
    super.onStart();

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            Dialog dialog = getDialog();
            if (dialog != null) {
            // Set Layout 
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            // Set Cancelable False
            setCancelable(false);

            // Set Focus Here <------------------------
            uiET_myTextView.requestFocus();
            }            
        }
    });
}


回答4:

Try removing your focus commands. They aren't necessary, Android should focus on the first field automatically?



回答5:

I'll refer to this older post.

If you just want your EditText to show a hint (for "what goes in here"), you might want to use the Android's XML-attribute hint (link).



回答6:

I also noticed that with android:selectAllOnFocus="true" seemed not to work, I used the mouse to select EditText in the emulator, but if I used my finger and touched it, as if on the phone, it worked. If you don't have a touch screen on your computer you may have to install your app on a physical device to test it. Android Studio may not recognize the mouse in this case