我的活动中有一个编辑文本。我正在手动输入数字,但是
int mystart = destinationNumber.getSelectionStart();
int myend = destinationNumber.getSelectionEnd();
numberText.getText().replace(Math.min(mystart, myend), Math.max(mystart, myend),
"1", 0, 1);它可以根据光标位置输入。
我的acitivity中有一个删除按钮,它可以根据光标位置删除单个字符。
numberText.getText().delete(myend - 1, mystart);但是当我选择整个文本并调用delete方法时,这个逻辑不能正常工作,它会给我IndexOutOfBoundsException,或者我选择4-5位数字并调用这个delete。
我想要与安卓联系人拨号盘号码输入field.Can相同的功能,有人能帮我弄清楚从编辑文本中删除单个数字和多个选定数字的正确逻辑是什么。
谢谢
发布于 2011-08-25 15:33:36
delete接收start作为第一个参数,接收end作为第二个参数,而不是相反。
发布于 2011-08-25 15:31:50
可能它的the、mystart或myend(可能是这个)的误差比numberText.lenght()更大或更小。
尝试使用文本长度为mystart和myend的Log.d( "“,”“),并检查是否需要myend -1或类似的内容。
https://stackoverflow.com/questions/7186556
复制相似问题