Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use EditText onTextChanged event when I press the number?

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().

like image 968
Ria Avatar asked Sep 15 '11 13:09

Ria


People also ask

Which method is used to get the string value from an EditText?

The method used to get the data from an EditText is getText().

How do I change my EditText value?

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.

How do I get the length of EditText content?

getText(). toString(). length()); } });


2 Answers

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();     }  }); 
like image 156
Khawar Raza Avatar answered Sep 22 '22 05:09

Khawar Raza


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.

like image 41
Roll no1 Avatar answered Sep 24 '22 05:09

Roll no1