使用下面的代码,我将替换所有字符(如果我的edittext包含四个a,这里都删除了所有字符),其中包含子字符串,并且光标位置在edittext字段中的第一个字符之前.
//getting cursor position
int end = t1.getSelectionEnd();
//getting the selected Text
String selectedStr = t1.getText().toString().substring(end-1, end);
//replacing the selected text with empty String and setting it to EditText
t1.setText(t1.getText().toString().replace(selectedStr, ""));
如何清除光标位置前的单个字符(如果我的光标位于edittext的中间)而不更改光标位置.
解决方法:
我觉得它迟到但仍然.
您可以使用SpannableStringBuilder.
//getting cursor position
int end = t1.getSelectionEnd();
//getting the selected Text
SpannableStringBuilder selectedStr=new SpannableStringBuilder(t1.getText());
//replacing the selected text with empty String and setting it to EditText
selectedStr.replace(end-1, end, "");
edittext1.setText(selectedStr);