0

On some devices, with this code, the EditText to_quantity is not updated in real time during the insertion of the value to be converted in the EditText from_quantity. What can be the problem?

EDIT TEXT from_quantity:

from_quantity.setOnKeyListener(new OnKeyListener(){ public boolean onKey(View v, int keyCode, KeyEvent event){ try{ if(!(Double.isNaN(Double.valueOf(from_quantity.getText().toString())))){ convert(from_quantity.getText().toString(), "to"); } }catch(NumberFormatException e){ Log.d("error", e.toString()); } return false; } }); from_quantity.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { //Clear Quantities from_quantity.setText(""); to_quantity.setText(""); return false; } }); 

EDIT TEXT to_quantity:

to_quantity.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { try{ if(!(Double.isNaN(Double.valueOf(to_quantity.getText().toString())))) { convert(to_quantity.getText().toString(), "from"); } } catch(NumberFormatException e) { Log.d("error", e.toString()); } return false; } }); to_quantity.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { //Clear Quantities from_quantity.setText(""); to_quantity.setText(""); return false; } }); 
1
  • What do you want to achieve bro? you can use on focus change listener on edit text and do stuffs there realtime when edittext gain or lose focus. Commented May 10, 2013 at 10:11

3 Answers 3

0

If you use the soft keyboard of your device, the OnKeyListener will not be called. You should use a TextWatcher.

onKeyListener not working with soft keyboard (Android)

Sign up to request clarification or add additional context in comments.

Comments

0

Some software keyboards do not launch onKey() events. Create a TextWatcher and set it as a listener to the EditText instead:

to_quantity.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String text = s.toString() ; try{ if(!(Double.isNaN(Double.valueOf(text)))) { convert(text, "from"); } } catch(NumberFormatException e) { Log.d("error", e.toString()); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); 

3 Comments

but in this way the two EditText, from_quantity and to_quantity, are not coordinated ie the added is distorted
I don't understand what it means not coordinated... Use a TextWatcher in both EditText fields, it's the same as what you were trying to do except that I used a TextWatcher instead of OnKeyListener. If the code inside it doesn't do what it was supposed to do, that's a different matter.
practically, in from_quantity I enter the number to convertitre and at the same time it is converted to to_quantity. But using the TextWatcher, the cursor is not simultaneous with edidtext to_quantity
0

if you wanna use soft keyboard you need to setFocusable to target view:

setFocusableInTouchMode(true); //Enable soft keyboard on touch for target view setFocusable(true); //Enable hard keyboard to target view 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.