Creating a Custom input type for android
my requirement is that, the user should only be able to type in digits 0 throu 9 , and that after every 4 characters, a "-" sign is automatically appended to the edittext.user should not be able to delete any part of the edittext except at end. Please suggest hoe 开发者_Python百科to do this.
The user should not be able to move the cursor to anywhere in the middle of the typed text, and be able to delete it. How to accomplish this? What event is called when the user moves the position of the cursor?
your first requirement is 0-9 is fulfill by set edit text property in XML user type number only and to count your text in edit-text set text watcher listener in edit text object and count word and there you can append "-" character.
I found a solution to my problem : user should not be able to move the cursor to anywhere in the middle of the typed text. We need to extend EditText
and add override the following function:
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
// TODO Auto-generated method stub
// this method will check if the cursor is moved. if yes then bring back
// the cursor to the end so that the user cannot delete anythign from
// the middle of the text(here sub id). Any editing will only be
// possible at the end of the text
if (selStart == selEnd) {
int length = getText().toString().length();
setSelection(length);
}
super.onSelectionChanged(selStart, selEnd);
}
精彩评论