Forgive me for asking what seems like an easy question, but I am new
to Android, and have spent hours getting this far, only to find a
problem running my code on the handset.
I am trying to force an EditText field ('Cost') to only allow:
Numbers, a Period and DEL
In addition, I want the ENTER key to fire a Button Click for
'CalculateBtn'.
I have the following code:
EditText txtCost = (EditText) findViewById(R.id.Cost);
txtCost.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View arg0, int arg1, KeyEvent
arg2) {
if(arg1==KeyEvent.KEYCODE_ENTER)
{
Button btnCalculate = (Button)
findViewById(R.id.CalculateBtn);
btnCalculate.performClick();
return true;
}
//else if(arg1 == 46 || arg1 > 47 || arg1 < 58)
else if(arg1==KeyEvent.KEYCODE_DEL ||
arg1==KeyEvent.KEYCODE_PERIOD || arg1==KeyEvent.KEYCODE_0 ||
arg1==KeyEvent.KEYCODE_1 || arg1==KeyEvent.KEYCODE_2 ||
arg1==KeyEvent.KEYCODE_3 || arg1==KeyEvent.KEYCODE_4 ||
arg1==KeyEvent.KEYCODE_5 || arg1==KeyEvent.KEYCODE_6 ||
arg1==KeyEvent.KEYCODE_7 || arg1==KeyEvent.KEYCODE_8 ||
arg1==KeyEvent.KEYCODE_9)
{
return false;
}
else
{
return true;
}
}
});
Testing in Eclipse, I thought everything was working fine, but when I
installed it on my handset (Nexus One) and ran it, I found that I
could type whatever characters I wanted using the on-screen keyboard!
It doesn't seem like this is a hard nut to crack, but I have so far
failed to find a solution.
Thanks for any pointers.
Alex
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en