How can I check an EditTextPreference value before

2019-09-19 07:11发布

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?

3条回答
别忘想泡老子
2楼-- · 2019-09-19 07:35

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 .

查看更多
Anthone
3楼-- · 2019-09-19 07:50

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.

查看更多
戒情不戒烟
4楼-- · 2019-09-19 07:52

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?

查看更多
登录 后发表回答