How is it possible to detect if a character is inputted into an EditText which makes it from 0 to 1 characters long and then perform some action?
You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus. This goes in your activity or fragment or wherever you have the EditTexts. The .... is just saying that you can put it anywhere else in the class.
Make EditText non editable in Android To use this just set android:inputType="none" and it will become non editable.
android:visibility This controls the initial visibility of the view.
Since you have a rather abstract question, let me attempt an equally generic answer:
In your onCreate()
, declare and cast your EditText
EditText editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(filterTextWatcher);
And then, outside the onCreate()
, setup the filterTextWatcher
like this:
private TextWatcher filterTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// DO THE CALCULATIONS HERE AND SHOW THE RESULT AS PER YOUR CALCULATIONS
int radius = 0;
radius = Integer.valueof(s.toString);
double area = Math.PI * radius * radius;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
EDIT:
Updated code with possible calculation. (UNTESTED CODE: I JUST TYPED IT IN WITHOUT TESTING IT. MODIFY WHERE NECESSARY)
Read more about TextWatcher's here
And here are a few examples to get you started:
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