0

I am trying to add a key listener for a edittext object. this is my .axml

<EditText
        android:id="@+id/amountTextBox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

and this is how i get the object EditText textbox=FindViewById(Resource.Id.amountTextBox);

I am trying to change the text when they press a key so it is formatted as a decimal number i.e. if they input 5 the outcome will be 0.05.

I tried using textbox.KeyPress but this only gets called when the back key or done key are pressed.

Can someone guide me in the right direction?

Catalina
  • 1,954
  • 1
  • 17
  • 25

2 Answers2

0

Instead of trying to do it in code, try adding this to your EditText object:

android:inputType="numberDecimal|numberSigned"
Daniel Frear
  • 1,459
  • 14
  • 22
  • i tried this but it doesn't have to outcome that i want. with this if the existing text it 0.00 and i press 5 the outcome would be 0.005 whereas i want it to be 0.05. – Catalina Oct 23 '13 at 10:36
  • OK - you could implement your own InputFilter as shown here: http://stackoverflow.com/questions/5357455/limit-decimal-places-in-android-edittext – Daniel Frear Oct 23 '13 at 11:43
  • that would be very useful if it was c# but unfortunately some of the methods just don't exist in c# such as Pattern.compile – Catalina Oct 23 '13 at 13:18
0

Use TextWatcher

editText.addTextChangedListener(new TextWathcer(){
      @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

        @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
        }

});
Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56