0
How to create a mask for monetary value in an editText, real in case it format this way to save in the database "20.99"? 1.99 20.99 300.99 1000.99 10000.99
0
How to create a mask for monetary value in an editText, real in case it format this way to save in the database "20.99"? 1.99 20.99 300.99 1000.99 10000.99
0
There is an event on android called addTextChangedListener
for the EditText
with the methods afterTextChanged
that will do an action when the text is changed, beforeTextChanged
which will do an action before the text is changed and onTextChanged
that will do a real-time action, and using the method of converting to $ post format https://stackoverflow.com/questions/2379221/java-currency-number-format you can make the following way:
EditText searchTo = (EditText)findViewById(R.id.medittext);
searchTo.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
searchTo.setText(formatDecimal(Float.parseFloat(searchTo.getText())));
}
});
Method of conversion to cash:
public String formatDecimal(float number) {
float epsilon = 0.004f; // 4 tenths of a cent
if (Math.abs(Math.round(number) - number) < epsilon) {
return String.format("%10.0f", number); // sdb
} else {
return String.format("%10.2f", number); // dj_segfault
}
}
hello, I implemented your idea but locked the app by clicking on editText that gets the value.
Then, you will have to put only numbers in the Edit text entry
Browser other questions tagged android edittext
You are not signed in. Login or sign up in order to post.
You can implement at hand as the friend commented above. But beware of performance in more complex masks. In the project I work I decided to ultilizar the lib https://github.com/santalu/maskara . I advise. .
– Yacov Rosenberg