I have an EditText
with "text = 0.00"
. When I press the number 3, it should be like 0.03
and the second time when I press the number 5, the text should be 0.35
. And 35.0
, 35.09
like this. The EditText
initially has the value of 0.00
.
These are all done with the same EditText
.
How do I achieve this? I have tried using addTextChangedListener()
with TextWatcher()
.
The method used to get the data from an EditText is getText().
This example demonstrates how do I set only numeric value for editText in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
getText(). toString(). length()); } });
You can also try this:
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) { doSomething(); } });
You have selected correct approach. You have to extend the class with TextWatcher and override afterTextChanged()
,beforeTextChanged()
, onTextChanged()
.
You have to write your desired logic in afterTextChanged()
method to achieve functionality needed by you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With