user2029541
user2029541

Reputation: 666

Android Edittext number only

I have a a Edittext field that is dynamically created I have it so that the input method is number only. However it is still possible to paste text into the field. Is there anyway of preventing this without having to do post validation?

My code to for input method is currently:

editText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

Upvotes: 3

Views: 967

Answers (3)

Radhey
Radhey

Reputation: 2219

use setInputType rather than setRawInputType like this:

edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

Upvotes: 0

DJhon
DJhon

Reputation: 1518

Whatever you have done is almost correct for copy-paste condition you can use this

    Field1.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      if(s.length() != 0)

   }
  });

Upvotes: 0

Marcus
Marcus

Reputation: 6717

Try using this method

.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

Upvotes: 4

Related Questions