Let EditText can input zero when zero is the first number

Now my EditText can enter the number. But I want to disallow zero entry when zero is the first number. How can I do?

+3


source to share


5 answers


This should help you:



        yourTextEdit.addTextChangedListener(new TextWatcher(){
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            if (yourTextEdit.getText().matches("^0") )
            {
                // Not allowed
                yourTextEdit.setText("");
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void afterTextChanged(Editable s){}
    }); 

      

+5


source


You can write logic using TextWatcher. Try google to view the text.

To start at the bottom, this is a piece of code.



.addTextChangedListener (new TextWatcher () {public void afterTextChanged (Editable s) {// Nothing} public void beforeTextChanged (CharSequence s, int start, int count, int after) {// Nothing} public void onTextChanged (CharSequence s, int start , int before, int count) {// Write your stuff here}});

0


source


you can achieve this with TextWatcher:

yourEditText.addTextChangedListener(new TextWatcher() {

        @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) {
            String tmp = s.toString().trim();
            if(tmp.length()==1 && tmp.equals("0"))
                s.clear();
        }
    });

      

0


source


This will help u

if(edittext.getText().toString().length()== 0 || Integer.valueOf(edittext.getText().toString())== 0){
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Please enter atleast 1 as value");
                builder.setCancelable(true);
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // TODO Auto-generated method stub
                            dialog.cancel();
                    }
                });
                builder.create().show();
            }

      

0


source


I tried this way and it works great!

  yourEditText.addTextChangedListener(new TextWatcher(){
                public void onTextChanged(CharSequence s, int start, int before, int count)
                {
//***  Use the below to lines ****
                    if (yourEditText.getText().startsWith("0") )
                        yourEditText.setText("");    
                }
                public void beforeTextChanged(CharSequence s, int start, int count, int after){}
                public void afterTextChanged(Editable s){}
            });

      

0


source







All Articles