I am developing a Point Of Sales app.
So I would like to let user to enter the purchase amount
Let's say User input 100000
but I want it to automatically show up 100,000
. and 1000000
become 1,000,000
The second problem is that, I don't want user to be able to input .
themselves.
Third problem is that since this is money, we can't let user to enter 0 in the beginning.
Any ideas?
So far I can only come up with inputType=numberDecimal
which is not really helpful.
Thank you very much
PS: I do not need any decimal places
if you want use in currency add addTextChangedListener
to your desired edittext then monitor changes and reformat it , here is sample code
private String current = "";
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().equals(current)){
[your_edittext].removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,.]", "");
double parsed = Double.parseDouble(cleanString);
String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));
current = formatted;
[your_edittext].setText(formatted);
[your_edittext].setSelection(formatted.length());
[your_edittext].addTextChangedListener(this);
}
}
You could implement one or more InputFilter s to enforce those constraints on your EditText
. It is possible to attach multiple filters on an EditText
by using it's setFilters
method.
You could also use TextWatcher s to achieve the same thing. However, using InputFilter
makes a little bit more sense as it allows you to alter the text without having to call the setText
method after each change you make in the input.
For your first problem follow this link Thousand separator
For your second problem add this to your editext
android:digits="0123456789"
android:inputType="numberDecimal"
And for your third problem you have to use TextWatcher like this
editText1.addTextChangedListener(new TextWatcher(){
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (editText1.getText().toString().matches("^0") )
{
// Not allowed
Toast.makeText(context, "not allowed", Toast.LENGTH_LONG).show();
editText1.setText("");
}
}
@Override
public void afterTextChanged(Editable arg0) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.