问题
I have currently a Preference in my application where the user is prompted to enter between 2 and 10 numerical values.
As this feature is only available for power users and beta testers, not for public release, I decided to let them enter a CSV value.
So, in my EditTextPreference, some users will enter: "1;20;30", some other will enter "1;10;10;10;10;10;10;10" etc...
After, in my code, I am just splitting these values to build an array and execute my code. I have to admit that's not very professional, but that just works!
String[] patternString = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("vibPattern", "0;500;500;500;500;500;500;500").split(";");
The main issue is that I would like to check the validity of the String while the user is writing it!
Does some of you have an idea how to achieve that?
回答1:
you need to use "addTextChangedListener" and act according to the text that was , well, changed...
if you wish, you can use regular expressions in order to check for validity .
回答2:
why not just programmatically add the amount of wished boxes, (for an example) have a spinner(drop down menu), from where you can choose the amount, and then add that amount of edittext with numerical only?
回答3:
In the EditText entry in your layout add the following:
android:inputType="number" android:digits="0123456789,"
Then on the addTextChangeListener implementations you only have to check if a user just entered a bunch of commas.
来源:https://stackoverflow.com/questions/10648824/how-can-i-check-an-edittextpreference-value-before-the-user-validate-it