How can i customise an EditText Box to just allow currency?
i'm new to development and i'm just starting my journey into the daunting world of validation. So far i've had it easy and been able to restricted the inputtype just with these two lines: android:inputType="phone" android:digits="1234567890."
Now i want to try and stop my user from entering digits past two decimal places. I think i either need to be working with an InputFilter or using onTextChange to check for开发者_StackOverflow社区 use of the decimal and digits thereafter( alas i don't have a clue how i would go about doing that either). I've got no experience with either of these and i'm having a hard time finding examples online that i can understand, If someone could tell me where to start that would be really appreciated, i think i'm on the right track but im not even sure how i'd go about checking characters let alone restricting them. Any answers welcome, thank you.
Create a class that implements TextWatcher
class CurrencyFormatterTextWatcher implements TextWatcher { @Override public synchronized void afterTextChanged(Editable text) { // [...] do your formatting here; beware that each // change to the text value will fire another textchanged event } }
then register it as a listener for textchanged events fired by your targeted EditText:
currencyFormatter = new CurrencyFormatterTextWatcher(); currencyEditText.addTextChangedListener(currencyFormatter);
精彩评论