EditText selectAll () does not select text, instead the cursor moves to position 0

My situation: I have one EditText, and under it I have a button called "select all". The goal is to allow the user to select all the text by clicking it.
When the button is pressed, I call selectAll()in EditText, but instead of selecting all the text, in some cases (usually when the cursor is already placed inside the text somewhere), the cursor moves to the position 0(beginning of the text) and the text remains unselected. Then press the button, then select all. But, of course, this should happen at the first click. From this problem: Android EditText selectAll () does not work if a single click on the same field in android 4.x only seems to be a bug in android 4.0 and above. (I didn’t find mentions in Google trackers).
Does anyone know of a way to overcome this problem? It seems that the task also applies to other selection methods, not just to selectAll(). (ps) This question is a kind of duplicate of the problem that I mentioned above. But I opened another question, because the author of this problem was satisfied and chose a partial answer (installation android:selectAllOnFocus="true"), while in my case and the script he does not help me and will not solve the problem.

Thank.

+4
source share
3 answers

The problem caused by IME. If the pointer drag the cursor, then the selection should be zero width.

You need to undo the drag pointer. This can be done by changing the text. For example replace:

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

. .

+2

:

yourEditText.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        //((EditText)v).selectAll();
        ((EditText)v).setSelection(startValue, stopValue);
    }
});

:

yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
    @Override
    public void onFocusChange(View v, boolean hasFocus){
        if (hasFocus){
            //((EditText)v).selectAll();
            ((EditText)v).setSelection(startValue, stopValue);
        }
    }
});

:

theEditText.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        EditText editText = (EditText)view;
        editText.setSelection(editText.getText().length()-1); // selects all the text
    }
});

:

theEditText.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        EditText editText = (EditText)view;
        editText.performLongClick();
    }
});

, ..:)

+1

. ( Xamarin #, Java ) :

private void InitFocus()
{
    Java.Lang.Runnable rable = new Java.Lang.Runnable(()=> 
    {
        EditText maleCount = FindViewById<EditText>(Resource.Id.txtMaleCount);
        maleCount.RequestFocus();
        maleCount.SelectAll();
    });
    new Handler().Post(rable);
}

2 : One - , , . , , txtMaleCount EditText, , ( , , ),

Obviously, the unintuitive part was the need for Postit. Doing this directly (in the user interface thread) did not seem to have any effect.

+1
source

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


All Articles