我在设计表单填写应用程序时在 EditText 中输入了文本。现在,如果我选择该文本的一部分,并希望删除/修改它,我不知道该怎么做。所有其他选项显示如何清除整个文本框。如何清除刚刚选中的文本。
最佳答案
EditText inputText=(EditText)findViewById(R.id.edit);
String inputString=inputText.getText().toString();
//To get the selected String
int selectionStart=inputText.getSelectionStart();
int selectionEnd=inputText.getSelectionEnd();
String selectedText = inputString.substring(selectionStart, selectionEnd);
if(!selectedText.isEmpty())
{
//Modify the selected StringHere
String modifiedString="...your modification logic here...";
//If you wish to delete the selected text
String selectionDeletedString=inputString.replace(selectedText,"");
inputText.setText(selectionDeletedString);
//If you wish to modify the selected text
String selectionModifiedString=inputString.replace(selectedText,modifiedString);
inputText.setText(selectionModifiedString);
}
关于java - 在android中的EdiTtext中删除/修改选定的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32179602/