开发者

IndexOutOfBoundsException in Edit Text for android

开发者 https://www.devze.com 2023-03-30 08:46 出处:网络
i have an edit text in my activity.i am entering numbers in it manually but int mystart = destinationNumber.getSelectionStart();

i have an edit text in my activity.i am entering numbers in it manually but

   int mystart = destinationNumber.getSelectionStart();
   int myend = destinationNumber.getSelectionEnd();
   numberText.getText().replace(Math.min(mystart, myend), Math.max(mystart, myend),
                        "1", 0, 1);

its entering fine according to the cursor position.

i have a delete button in my acitivity which deletes single character according to cursor postion.

numberText.getText().delete(myend - 1, mystart);

But this logic is not working properly when i select whole text and call delete method it gives me IndexOutOfBoundsException OR i select 4-5 digits and c开发者_如何学JAVAall this delete.

I want the same functionality as android contact dialpad number enter field.Can someone help me figure out what is the correct logic to delete single digit from edittext and multiple selected digits as well.

Thanks


delete receives the start as first parameter and end as second, not the other way around.


Probably the error its taht mystart or myend(probably this) are bigger or smoller than numberText.lenght().

Try to put a Log.d("","" ) with the lenght of the text, mystart and myend and check if you need a myend -1 or something like that.

0

精彩评论

暂无评论...
验证码 换一张
取 消