Never mind. Figured it out. EditText txtInput = (EditText) findViewById(R.id.txtInput); txtInput.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable edt) { String temp = edt.toString(); int posDot = temp.indexOf("."); if (posDot <= 0) return; if (temp.length() - posDot - 1 > 2) { edt.delete(posDot + 3, posDot + 4); } } public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {} public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {} }); On Mar 17, 11:18 pm, frizzo <rg...@vbrad.com> wrote: > I am trying to allow the user to enter a price (e.g. 3.99), so a > number with a max of 2 decimal places. I am not quite sure how to > pull this off. > > I've setup a KeyListener, but the OnKey event that it kicks off allows > listeners to get a chance to respond before the target view. As a > result, I don't really know what the string in the EditText will end > up looking like. For instance, I don't know whether the user entered > a digit into the beginning of the string or tacked it on at the end. > > Is there an event that kicks off after the EditText has processed > input and then I can evaluate the string? > > I got the following, but it suffers from the issue I described above. > > EditText txtDecimal = (EditText) findViewById > (R.id.txtDecimal); > txtDecimal.setOnKeyListener(new OnKeyListener() { > public boolean onKey(View v, int keyCode, KeyEvent event) > { > > EditText txtDecimal = (EditText) findViewById > (R.id.txtDecimal); > String temp = txtDecimal.getText().toString(); > > int posDot = temp.indexOf("."); > if (posDot <= 0) return false; > > if (temp.length() - posDot > 2) > return true; > > return false; > } > }); --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---