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

问题描述 投票:4回答:5

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

android android-edittext
5个回答
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
投票

这将有助于你

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
投票

你可以用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
投票

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

  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
投票

您可以使用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
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.