android edittext

3

我有一个 EditText:

EditText et1=(EditText)findViewById(R.id.EditText01);

当我想要清空 EditText 时,我使用 et1.setText(""); 但是如果我需要从最后一个字符开始逐个删除,我没有解决方案,请问您能给我一个解决方案吗?


你是用这种方式清除以产生动画效果的吗? - ankitjaininfo
3个回答

4

Do you know about SubString?

String contents = et1.getText().toString();
et1.setText(contents.substring(0, contents.length()-2));

1

如果你知道你想要清除的内容,你可以从EditText中获取文本,然后使用string.substring(0,numOfEND)从末尾删除指定数量的字符。


1
如果您的editText具有焦点,那么您可以像这样通过退格键从光标处删除字符:
if (et1.getSelectionStart() > 0) { //check that you are not deleting from the zero point
    et1.getText().delete(et1.getSelectionStart()-1, et1.getSelectionEnd());
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接