当零是第一个数字时,让EditText输入零

时间:2012-03-29 12:47:32

标签: android android-edittext

现在,我的EditText只能输入数字。但是我想禁止在零是第一个数字时输入零。我该怎么办?

5 个答案:

答案 0 :(得分:6)

这可以帮到你:

        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){}
    }); 

答案 1 :(得分:1)

这将有助于你

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();
            }

答案 2 :(得分:0)

您可以使用TextWatcher编写逻辑。尝试谷歌搜索textwatcher

从下面开始是代码段。

<textboxobj>.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
        }
    });

答案 3 :(得分:0)

您可以使用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();
        }
    });

答案 4 :(得分:0)

我试过这样,它运作正常!

  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){}
            });