Place cursor at the end of text in EditText
我正在更改
但是当我更改文本时,光标移动到
我需要光标位于文本的末尾。
如何将光标移动到
试试这个:
1 2 | EditText et = (EditText)findViewById(R.id.inbox); et.setSelection(et.getText().length()); |
有一个名为append for ediitext的函数,它将字符串值附加到当前的edittext值,并将光标放在值的末尾。
您可以将字符串值作为当前ediitext值本身并调用append();
1 | myedittext.append("current_this_edittext_string"); |
科特林:
将光标设置为起始位置:
1 2 | val editText = findViewById(R.id.edittext_id) as EditText editText.setSelection(0) |
将光标设置为EditText的末尾:
1 2 | val editText = findViewById(R.id.edittext_id) as EditText editText.setSelection(editText.getText().length()) |
Code下面是将光标放在第二个字符后面:
1 2 | val editText = findViewById(R.id.edittext_id) as EditText editText.setSelection(2) |
JAVA:
将光标设置为起始位置:
1 2 | EditText editText = (EditText)findViewById(R.id.edittext_id); editText.setSelection(0); |
将光标设置为EditText的末尾:
1 2 | EditText editText = (EditText)findViewById(R.id.edittext_id); editText.setSelection(editText.getText().length()); |
Code下面是将光标放在第二个字符后面:
1 2 | EditText editText = (EditText)findViewById(R.id.edittext_id); editText.setSelection(2); |
如果您之前调用
所以,对我来说这个代码有效:
1 2 3 4 5 6 7 | editText.setText("text"); editText.post(new Runnable() { @Override public void run() { registerPhone.setSelection("text".length()); } }); |
编辑05/16/2019:现在我正在使用Kotlin扩展:
1 2 3 | fun EditText.placeCursorToEnd() { this.setSelection(this.text.length) } |
然后 - editText.placeCursorToEnd()。
您也可以将光标放在
1 2 3 | EditText et = (EditText)findViewById(R.id.textview); int textLength = et.getText().length(); et.setSelection(textLength, textLength); |
1 2 3 4 5 6 7 | editText.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { editText.setSelection(editText.getText().length()); return false; } }); |
这是另一种可能的解决方案
1 | et.append(""); |
如果它因任何原因不起作用,请尝试此解决方案:
1 | et.setSelection(et.getText().length()); |
在我的情况下,我创建了以下kotlin ext。功能,可能对某人有用
1 2 3 4 | private fun EditText.focus(){ requestFocus() setSelection(length()) } |
然后使用如下
1 | mEditText.focus() |
您应该能够使用EditText的方法setSelection()实现这一点,请参阅此处
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * 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); |
它对我有用!
我认为这可以实现你想要的。
1 2 | Editable etext = mSubjectTextEditor.getText(); Selection.setSelection(etext, etext.length()); |
如果您的EditText不清楚:
1 2 | editText.setText(""); editText.append("New text"); |
这个问题已经过时并且已经回答了,但我认为如果您想使用Android最新发布的DataBinding工具,只需在XML中设置它就可能有用这个答案:
1 2 3 4 5 6 7 8 9 10 | <data> <variable name="stringValue" type="String"/> </data> ... <EditText ... android:text="@={stringValue}" android:selection="@{stringValue.length()}" ... /> |
Hello Try This
1 2 3 4 5 6 | <EditText android:id="@+id/edt_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!" android:cursorVisible="true"/> |
editText.setSelection(editText.getText().length()); // End point
如果要选择所有文本并只输入新文本而不是旧文本,则可以使用
1 | android:selectAllOnFocus="true" |
这有效
1 2 | Editable etext = edittext.getText(); Selection.setSelection(etext,edittext.getText().toString().length()); |
1 2 | EditText editText = findViewById(R.id.editText); editText.setSelection(editText.getText().length()); |
这将光标放在EditText的末尾。基本上
用于将光标设置在起始位置(0)。
我测试过的所有其他代码都不能正常工作,因为用户仍然可以将插入/光标放在字符串中间的任何位置(例如:12 | 3.00 - 其中|是光标)。只要在EditText上发生触摸,我的解决方案总是将光标放在字符串的末尾。
最终的解决方案是:
1 2 3 4 5 6 7 8 9 10 | // 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"/> |
@串/量="0.00"
@串/ zero_value ="0.00"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | // 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) { } }); |
希望能帮助到你!
这可以安全地解决问题:
1 2 3 4 | editText.setText(""); if (!TextUtils.isEmpty(text)) { editText.append(text); } |
类似于@Anh Duy的答案,但它对我没用。我还需要光标移动到最后只有当用户点击编辑文本并仍然能够选择光标的位置后,这是唯一适用于我的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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; } }); |
1 | etSSID.setSelection(etSSID.getText().length()); |
如果要将光标放在EditText视图中的文本末尾
1 2 3 4 5 | EditText rename; String title ="title_goes_here"; int counts = (int) title.length(); rename.setSelection(counts); rename.setText(title); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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) { } } |
在XML文件中使用此CustomEditText,这将起作用。我测试了这个,它对我有用。
1 2 3 | EditText editText=findViewById(R.id.et_id); editText.requestFocus(); |