0

I have EditBox where I have to check if the number entered by the user is correct or not. For doing so I want to call such an event that allows the user to type a number in the EditBox whenever the number is correct, otherwise, if the number is not correct it doesn't allow the user to type in the EditBox. How That can be done? Any example will be helpful for me.

iknow
  • 8,358
  • 12
  • 41
  • 68
AndroidDev
  • 4,521
  • 24
  • 78
  • 126

4 Answers4

4

use onTextChangedListener() and validate inside its beforeTextChangedMethod() .. something like this..

 ((EditText)findViewById(R.id.et_testo)).addTextChangedListener(new TextWatcher() {

    public void afterTextChanged(Editable s) {
        ((TextView)findViewById(R.id.numcaratteri)).setText(String.format(getString(R.string.caratteri), s.length()));

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // Validate here

    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub

    }

});
ngesh
  • 13,398
  • 4
  • 44
  • 60
  • Actually During Typing in the edit box only i want to validate the number and i don't allow the user to type wrong date. Means the user can only type correct number. I don't allow user to type the wrong number. – AndroidDev Apr 26 '12 at 07:02
  • @Anshuman .. then don't clear text inside the method if its invalid else set it... – ngesh Apr 26 '12 at 07:10
  • Can u show me with an example..please i am stuck in it for an hour – AndroidDev Apr 26 '12 at 07:12
2
final Pattern pattern = Pattern.compile("[0-9]");
editText.addTextChangedListener(new TextWatcher() {

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        Matcher matcher = pattern.matcher(s);
        if (matcher.find())
        {
            s.replace(0, s.length(), s.toString().replaceAll("[0-9]", ""));
        }
    }

    public void beforeTextChanged(CharSequence s, int start,
            int before, int count) {
        // TODO Auto-generated method stub
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub
    }

});
C. Leung
  • 6,218
  • 3
  • 20
  • 32
1

You will have to use TextWatcher.

You can Just Go HERE. Example is also given here, you can refer it.

Bhavin
  • 6,020
  • 4
  • 24
  • 26
0

you can use 2 ways
1. textwatcher
2. Inputfilter
i am attaching the code which includes both

public class MainActivity extends Activity {   
EditText editTxt;  
private TextView regresult;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    editTxt =(EditText)findViewById(R.id.editID);
    regresult = (TextView)findViewById(R.id.txtID);
    String urName=editTxt.getText().toString();

    editTxt.setFilters(new InputFilter[]{new DecimalDigitsInputFilter()});      
    editTxt.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {    
        }           
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }           
        @Override
        public void afterTextChanged(Editable s) {
              if (editTxt.getText().toString().matches("(^([0-9]{0,3})?)(\\.[0-9]{0,1})?$"))
                {
                    regresult.setText("");
                }
                else
                {
                    regresult.setText("invalid number");
                }
        }
    });
}}  


class DecimalDigitsInputFilter implements InputFilter
{   
Pattern mPattern;

public DecimalDigitsInputFilter()
{
    mPattern = Pattern.compile("(^([0-9]{0,2})?)(\\.[0-9]{0,1})?$"); //here u can give your required pattern
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    // TODO Auto-generated method stub
     Matcher matcher = mPattern.matcher(dest);
        if(!matcher.matches())
        {
            return "";
        }
        return null;
}}
sush
  • 476
  • 1
  • 7
  • 20