在Android中,如何针对EditText禁用“登录"按钮? [英] In Android, how to make Login button disable with respect to EditText?

查看:114
本文介绍了在Android中,如何针对EditText禁用“登录"按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If EditText is empty then Login Button has to be disabled. And if EditText has some texts then Login Button has to be enabled. Well you can see this method on Instagram Login.

两个字段均为空,Button登录被禁用.

Both fields are empty, Sign in Button is DISABLED.

此处密码"字段为空,因此仍禁用登录Button".

Here Password field is empty, so still Sign inButton is DISABLED.

用户名"和密码"字段都不为空,因此启用登录Button".

Here both Username and Password field is not empty, So Sign inButton is ENABLED.

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

how to achieve these steps?? here is my code and it doesn't work..

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

推荐答案

这是您要查找的内容:

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

这篇关于在Android中,如何针对EditText禁用“登录"按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆