10

如果EditText为空,则Button必须禁用登录。如果有一些文本,则必须启用EditText登录。Button好吧,您可以在 Instagram Login 上看到此方法。

两个字段都为空,登录Button已禁用。

在此处输入图像描述

此处密码字段为空,因此登录仍处于Button禁用状态。

在此处输入图像描述

这里用户名和密码字段都不是空的,所以登录Button是启用的。

在此处输入图像描述

如何实现这些步骤?这是我的代码,它不起作用..

EditText et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_check);
    et1 = (EditText) findViewById(R.id.editText1);
    et2 = (EditText) findViewById(R.id.editText2);
    Button b = (Button) findViewById(R.id.button1);

    String s1 = et1.getText().toString();
    String s2 = et2.getText().toString();

    if(s1.equals("")|| s2.equals("")){
        b.setEnabled(false);
    } else {
        b.setEnabled(true);
    }
}
4

9 回答 9

33

继承人你在找什么:

private EditText et1,et2;
//  create a textWatcher member
private TextWatcher mTextWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    }

    @Override
    public void afterTextChanged(Editable editable) {
        // check Fields For Empty Values
        checkFieldsForEmptyValues();
    }
};

void checkFieldsForEmptyValues(){
    Button b = (Button) findViewById(R.id.button1);

    String s1 = et1.getText().toString();
    String s2 = et2.getText().toString();

    if(s1.equals("")|| s2.equals("")){
        b.setEnabled(false);
    } else {
        b.setEnabled(true);
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_check);
    et1 = (EditText) findViewById(R.id.editText1);
    et2 = (EditText) findViewById(R.id.editText2);


    // set listeners
    et1.addTextChangedListener(mTextWatcher);
    et2.addTextChangedListener(mTextWatcher);

    // run once to disable if empty
    checkFieldsForEmptyValues(); 
}
于 2013-02-21T13:23:12.350 回答
5

您需要实现TextWatcherEditText实现结果。

EditText et1, et2;
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et1 = (EditText) findViewById(R.id.editText1);
    et2 = (EditText) findViewById(R.id.editText2);
    b = (Button) findViewById(R.id.button1);

    checkValidation();

    et1.addTextChangedListener(mWatcher);
    et2.addTextChangedListener(mWatcher);
}

private void checkValidation() {
    // TODO Auto-generated method stub

    if ((TextUtils.isEmpty(et1.getText()))
            || (TextUtils.isEmpty(et2.getText())))
        b.setEnabled(false);
    else
        b.setEnabled(true);

}

TextWatcher mWatcher = new TextWatcher() {

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

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
};
于 2013-02-21T13:11:20.250 回答
3

您需要EditText使用TextWatcher对象跟踪用户的操作:

myEditText.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {

            }

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

            }

            @Override
            public void afterTextChanged(Editable s)
            {
                if (s.length() > 1)
                {
                    //enable button
                } else
                    //disable
            }
        });
于 2013-02-21T13:11:48.013 回答
2

试试这个:

EditText et1,et2;
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_login_check);
  et1 = (EditText) findViewById(R.id.editText1);
  et2 = (EditText) findViewById(R.id.editText2);
  b = (Button) findViewById(R.id.button1);


  et1.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

                            String s1 = et1.getText().toString();
                            String s2 = et2.getText().toString();

                             if(s1.equals("") && s2.equals("")){
                                   b.setEnabled(false);
                            } 
                            else if(!s1.equals("")&&s2.equals("")){
                                   b.setEnabled(false);
                            }
                            else if(!s2.equals("")&&s1.equals(""){
                                   b.setEnabled(false);
                            }
                            else {
                            b.setEnabled(true);
                          }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });
  et2.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

                            String s1 = et1.getText().toString();
                            String s2 = et2.getText().toString();

                            if(s1.equals("") && s2.equals("")){
                                   b.setEnabled(false);
                            } 
                            else if(!s1.equals("")&&s2.equals("")){
                                   b.setEnabled(false);
                            }
                            else if(!s2.equals("")&&s1.equals(""){
                                   b.setEnabled(false);
                            }
                            else {
                            b.setEnabled(true);
                          }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

}
于 2013-02-21T13:14:45.283 回答
2

您需要附加一个 TextWatcher,只要其中一个 EditText 字段中的文本发生更改,就会调用该 TextWatcher。

private EditText mName;
private EditText mPassword;
private Button mButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login_check);
    mName = (EditText) findViewById(R.id.editText1);
    mPassword = (EditText) findViewById(R.id.editText2);
    mButton = (Button) findViewById(R.id.button1);

    mName.addTextChangedListener(mWatcher);
    mPassword.addTextChangedListener(mWatcher);
}

private TextWatcher mWatcher = new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        boolean nameNotEmpty = mName.getText().length()>0;
        boolean pwNotEmpty = mPassword.getText().length()>0;
        mButton.setEnabled(nameNotEmpty && pwNotEmpty);
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}
};
于 2013-02-21T13:26:19.443 回答
1
private TextWatcher mPhoneNumberEtWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (charSequence.length() >= 10) {
            mPhoneImg.setImageDrawable(getResources().getDrawable(R.drawable.phone_activate));

            if (mPasswordEt.getText().toString().length() >= 5) {
                mLoginBtn.setEnabled(true);
            }

        } else {
            mPhoneImg.setImageDrawable(getResources().getDrawable(R.drawable.phone));
            mLoginBtn.setEnabled(false);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {
    }
};

mPhoneNumberEt.addTextChangedListener(mPhoneNumberEtWatcher);

您应该使用 TextWatcher。这将在输入用户后调用方法。您可以检查编辑文本的长度和其他内容。

于 2013-02-21T13:13:40.113 回答
0

试试这个

  if(s1.equals("") && s2.equals(""))
    {
        b.setEnabled(true);
        // to change color of the button you need to apply style to the button[here refer custom bg][1]
    }
    else
    {
        b.setEnabled(false);
       //do nothing or display toast msg
    }
于 2013-02-21T13:12:11.550 回答
0

我只想补充一点,如果是密码(或类似密码)并且证明文本长度的功能(请参阅其他答案)被调用 ,则检查将不起作用InputTypeEditText

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {...}

所以重要的是从

@Override 
public void afterTextChanged(Editable s) {...}
于 2015-02-27T02:19:49.127 回答
0

嘿,如果想使用代码剪切,如果 editText1_id 和 editText1_passcode 为 4 位数字,则按钮需要更改颜色

checkValidation();

    editText1_id.addTextChangedListener(mWatcher);
    editText1_passcode.addTextChangedListener(mWatcher);
}

private void checkValidation() {
    // TODO Auto-generated method stub

    if ((TextUtils.isEmpty(editText1_id.getText()))
            || (TextUtils.isEmpty(editText1_passcode.getText())))
        loginbtn.setEnabled(false);
    else
        loginbtn.setEnabled(true);

}


TextWatcher mWatcher = new TextWatcher() {

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

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
};

}
于 2017-07-02T10:43:36.227 回答