Reputation: 941
I am using OnKeyListener method of editText in android but it seems that device does not call its listener some times.Here is my code I am using .The problem is some times it comes to delete button even case but doesn't do what it should and sometimes onKeyListener doesn't even get called.Is there any issue with this method?Should I swich to textwatcher ?
cp_confirm_pin_code1.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.e("keycode action confirm pin1",""+keyCode);
if(event.getAction()==KeyEvent.ACTION_DOWN)
if(keyCode == KeyEvent.KEYCODE_DEL){
//this is for backspace
Log.e("back space for 1", "back space for 1");
cp_confirm_pin_code1.setText("");
}
else{
Log.e("other numbers 1", "other numbers 1 ");
String text=null;
Log.e("code", ""+keyCode);
switch (keyCode) {
case KeyEvent.KEYCODE_0:
text="0";
break;
case KeyEvent.KEYCODE_1:
text="1";
Log.e("text in 1",""+text);
break;
case KeyEvent.KEYCODE_2:
text="2";
break;
case KeyEvent.KEYCODE_3:
text="3";
break;
case KeyEvent.KEYCODE_4:
text="4";
break;
case KeyEvent.KEYCODE_5:
text="5";
break;
case KeyEvent.KEYCODE_6:
text="6";
break;
case KeyEvent.KEYCODE_7:
text="7";
break;
case KeyEvent.KEYCODE_8:
text="8";
break;
case KeyEvent.KEYCODE_9:
text="9";
break;
case KeyEvent.KEYCODE_BACK:
getActivity().finish();
break;
default:
Log.e("default", "default");
break;
}
Log.e("text vallue", "text value"+text);
if(text!=null){
Log.e("requesting focus 1", "requesting focus 1");
cp_confirm_pin_code1.setText(text);
cp_confirm_pin_code2.requestFocus();
}
}
return true;
}
});
Upvotes: 0
Views: 1880
Reputation: 4091
onKeyListener only gets invoked with events from the hardware keyboard.So I think it is better to use TextWatcher.
The documentation states that the key events will only be propagated for the hardware key strokes, not software.
http://developer.android.com/reference/android/view/View.OnKeyListener.html
The device manufacturers are actually being discouraged to propagate soft keyboard events through key listeners, although it is completely up to the manufacturer to honour that or to actually treat the soft and hard keyboards with equal terms.
Starting from Android 4.2.2, Android system itself will not support key stoke events for the soft keyboards at all, so even the manufacturers will not be able to choose their way.
So the only foolproof option here is to implement your own IME (soft keyboard), and handle the keystrokes yourself.
TextWatcher can be used mostly to replace the key listeners, however editText.setText(...); will also trigger the TextWatcher events, so if one is interested in typed keys only then probably TextWatcher is not a solution either.
Please be cautious when using TextWatcher with AutocomleteTextView or EditText. Do not modify text in the AutocompleteTextView / EditText's content from within TextWatcher events, cause otherwise you'll most probably end up in an infinite event/listening loop.
Upvotes: 1