I have 4 EditTexts
in my activity. Each one has an OnClickListener
that when called will check a checkbox for me. However, when I first press on the EditText
, the EditText
turns blue, but the checkbox does not check. It is only when I press on it again that the OnClickListener
takes effect and checks the box.
Does anyone know why this is?
Example code:
public View.OnClickListener respiratoryOnClickListenerv(final View v) {
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox respiratoryCheckbox = (CheckBox) v.findViewById(R.id.vitals_2);
respiratoryCheckbox.setChecked(true);
respiratoryCheckbox.callOnClick();
}
};
return listener;
}
respiratoryEditText.setOnClickListener(respiratoryOnClickListenerv(rootView));
When a user interacts with a UI element the various listeners are called in a top down order. (For example: OnTouch -> OnFocusChange -> OnClick.) If a listener has been defined (with setOn...Listener) and it consumes this event: the lower priority listeners will not be called. By its nature the first time you touch an EditText it receives focus with OnFocusChangeListener so that the user can type. The action is consumed here therefor OnClick is not called. Each successive touch doesn't change the focus so the event trickles down to the OnClickListener.
From that, you have three choices:
1) Set the focusable attribute to false in your XML:
android:focusable="false"
Now the OnClickListener will fire every time it is clicked. But this makes the EditText useless since the user can no longer enter text.
2) Implement an OnFocusChangeListener along with the OnClickListener:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
//Do your work
}
}
});
Together you can catch every touch event on your EditText.
3) Implement an OnTouchListener by itself:
editText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_UP == event.getAction())
Toast.makeText(getApplicationContext(), "onTouch: Up", Toast.LENGTH_SHORT).show();
return false;
}
});
This will execute every time the EditText is touched. Notice that this event returns a boolean. Returning false means that the event will continue trickle down and reach the built in onFocusChangeListener allowing it to receive text.
Hope this help!
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