我正在更改keyListener上的EditText的值。
但是当我更改文本时,光标移动到EditText的开头。 我需要光标在文本的末尾。
如何将光标移动到EditText文本的末尾。
我正在更改keyListener上的EditText的值。
但是当我更改文本时,光标移动到EditText的开头。 我需要光标在文本的末尾。
如何将光标移动到EditText文本的末尾。
当前回答
我测试过的所有其他代码都没有很好地工作,因为用户仍然可以将插入符号/光标放在字符串中间的任何地方(例如:12|3.00 -其中|是光标)。我的解决方案总是在EditText上发生触摸时将光标放在字符串的末尾。
最终的解决方案是:
// 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) {
}
});
希望能有所帮助!
其他回答
你也可以像这样把光标放在EditText视图中文本的末尾:
EditText et = (EditText)findViewById(R.id.textview);
int textLength = et.getText().length();
et.setSelection(textLength, textLength);
在我的情况下,我创建了以下Kotlin ext.函数,可能对某人有用:
private fun EditText.focus(){
requestFocus()
setSelection(length())
}
然后按如下方式使用:
mEditText.focus()
我认为这可以达到你想要的效果。
Editable etext = mSubjectTextEditor.getText();
Selection.setSelection(etext, etext.length());
试试这个:
<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()); // Set cursor end point
editText.accessibilityDelegate = object : View.AccessibilityDelegate() {
override fun sendAccessibilityEvent(host: View?, eventType: Int) {
super.sendAccessibilityEvent(host, eventType)
if (eventType == TYPE_VIEW_TEXT_SELECTION_CHANGED) {
editText.setSelection(editText.length())
}
}
}