I am changing the value of an EditText
on keyListener
.
But when I change the text the cursor is moving to the beginning of the EditText
. I need the cursor to be at the end of the text.
How to move the cursor to the end of the text in a EditText
.
转载于:https://stackoverflow.com/questions/6217378/place-cursor-at-the-end-of-text-in-edittext
You should be able to achieve that with the EditText's method setSelection(), see here
Try this:
EditText et = (EditText)findViewById(R.id.inbox);
et.setSelection(et.getText().length());
There is a function called append for ediitext which appends the string value to current edittext value and places the cursor at the end of the value. You can have the string value as the current ediitext value itself and call append();
myedittext.append("current_this_edittext_string");
i think this can achieve what you want.
Editable etext = mSubjectTextEditor.getText();
Selection.setSelection(etext, etext.length());
You could also place the cursor at the end of the text in the EditText
view like this:
EditText et = (EditText)findViewById(R.id.textview);
int textLength = et.getText().length();
et.setSelection(textLength, textLength);
If you called setText
before and the new text didn't get layout phase call setSelection
in a separate runnable fired by View.post(Runnable)
(repost from this topic).
So, for me this code works:
editText.setText("text");
editText.post(new Runnable() {
@Override
public void run() {
registerPhone.setSelection("text".length());
}
});
This is another possible solution:
et.append("");
Just try this solution if it doesn't work for any reason:
et.setSelection(et.getText().length());
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
editText.setSelection(editText.getText().length());
return false;
}
});
/**
* Set cursor to end of text in edittext when user clicks Next on Keyboard.
*/
View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b) {
((EditText) view).setSelection(((EditText) view).getText().length());
}
}
};
mEditFirstName.setOnFocusChangeListener(onFocusChangeListener);
mEditLastName.setOnFocusChangeListener(onFocusChangeListener);
It work good for me!
similar to @Anh Duy's answer, but it didnt work for me. i also needed the cursor to move to the end only when the user taps the edit text and still be able to select the position of the cursor afterwards, this is the only code that has worked for me
boolean textFocus = false; //define somewhere globally in the class
//in onFinishInflate() or somewhere
editText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editText.onTouchEvent(event);
if(!textFocus) {
editText.setSelection(editText.getText().length());
textFocus = true;
}
return true;
}
});
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
textFocus = false;
}
});
This does the trick safely:
editText.setText("");
if (!TextUtils.isEmpty(text)) {
editText.append(text);
}
If your EditText is not clear:
editText.setText("");
editText.append("New text");
If you want to select all text and just entering new text instead of old one, you can use
android:selectAllOnFocus="true"
All the other codes I've tested didn't work good due to the fact that the user could still place the caret/cursor wherever in the middle of the string (ex.: 12|3.00 - where | is the cursor). My solution always puts the cursor in the end of the string whenever the touch occurs on the EditText.
The ultimate solution is:
// For a EditText like:
<EditText
android:id="@+id/EditTextAmount"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="@string/amount"
android:layout_weight="1"
android:text="@string/zero_value"
android:inputType="text|numberDecimal"
android:maxLength="13"/>
@string/amount="0.00" @string/zero_value="0.00"
// Create a Static boolean flag
private static boolean returnNext;
// Set caret/cursor to the end on focus change
EditTextAmount.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View editText, boolean hasFocus) {
if(hasFocus){
((EditText) editText).setSelection(((EditText) editText).getText().length());
}
}
});
// Create a touch listener and put caret to the end (no matter where the user touched in the middle of the string)
EditTextAmount.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View editText, MotionEvent event) {
((EditText) editText).onTouchEvent(event);
((EditText) editText).setSelection(((EditText) editText).getText().length());
return true;
}
});
// Implement a Currency Mask with addTextChangedListener
EditTextAmount.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String input = s.toString();
String output = new String();
String buffer = new String();
String decimals = new String();
String numbers = Integer.toString(Integer.parseInt(input.replaceAll("[^0-9]", "")));
if(returnNext){
returnNext = false;
return;
}
returnNext = true;
if (numbers.equals("0")){
output += "0.00";
}
else if (numbers.length() <= 2){
output += "0." + String.format("%02d", Integer.parseInt(numbers));
}
else if(numbers.length() >= 3){
decimals = numbers.substring(numbers.length() - 2);
int commaCounter = 0;
for(int i=numbers.length()-3; i>=0; i--){
if(commaCounter == 3){
buffer += ",";
commaCounter = 0;
}
buffer += numbers.charAt(i);
commaCounter++;
}
output = new StringBuilder(buffer).reverse().toString() + "." + decimals;
}
EditTextAmount.setText(output);
EditTextAmount.setSelection(EditTextAmount.getText().length());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
/*String input = s.toString();
if(input.equals("0.0")){
EditTextAmount.setText("0.00");
EditTextAmount.setSelection(EditTextAmount.getText().length());
return;
}*/
}
@Override
public void afterTextChanged(Editable s) {
}
});
Hope it helps!
This works
Editable etext = edittext.getText();
Selection.setSelection(etext,edittext.getText().toString().length());
Kotlin:
set the cursor to the starting position:
val editText = findViewById(R.id.edittext_id) as EditText
editText.setSelection(0)
set the cursor to the end of the EditText:
val editText = findViewById(R.id.edittext_id) as EditText
editText.setSelection(editText.getText().length())
Below Code is to place the cursor after the second character:
val editText = findViewById(R.id.edittext_id) as EditText
editText.setSelection(2)
JAVA:
set the cursor to the starting position:
EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(0);
set the cursor to the end of the EditText:
EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(editText.getText().length());
Below Code is to place the cursor after the second character:
EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(2);
public class CustomEditText extends EditText {
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context) {
super(context);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
this.setSelection(this.getText().length());
}
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
}
}
User this CustomEditText in your XML file, this will work. I have tested this and it is working for me.
editText.setSelection
is the magic here. Basically selection gives you place cursor at any position you want.
EditText editText = findViewById(R.id.editText);
editText.setSelection(editText.getText().length());
This places cursor to end of EditText. Basically editText.getText().length()
gives you text length. Then you use setSelection
with length.
editText.setSelection(0);
It is for setting cursor at start position (0).
Hello Try This
<EditText
android:id="@+id/edt_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:cursorVisible="true"/>
EditText editText = findViewById(R.id.editText); editText.setSelection(editText.getText().length()); // End point
Cursor
The question is old and answered, but I think it may be useful to have this answer if you want to use the newly released DataBinding tools for Android, just set this in your XML :
<data>
<variable name="stringValue" type="String"/>
</data>
...
<EditText
...
android:text="@={stringValue}"
android:selection="@{stringValue.length()}"
...
/>
In my case I created the following kotlin ext. function, may be useful to someone
private fun EditText.focus(){
requestFocus()
setSelection(length())
}
Then use as follows
mEditText.focus()