How to set focus to the text after the hint in Edi

2019-06-10 00:50发布

问题:

I'm interested in you can set the focus to the text after the prompt to EditText? If no such attribute for xml layout? At the moent I still looked like this.

need

EDIT :
The fastest and working answer given Asok
I also found a similar way:
EditText.append("60"); // 60 or your text in EditText

回答1:

If I understand correctly you are looking to place the cursor behind the hint text, if this is correct then it is not possible, you'll have to remove hint and use setText instead, like so:

EditText et = (EditText)findViewById(R.id.sixtyseconds);
et.setText("60");
et.setSelection(et.getText().length());


回答2:

A hint is no real text, it disappears after the user types something. Assuming the cursor would be at the end of the hint what behaviour would you expect when the user presses a button and the hint disappears?

What you can do is set a default text in the XML via

android:text="60"

or in code via

editText.setText("60");

and on focus jump to the end of the EditText via

editText.setSelection(editText.getText().length());