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

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

问题描述

如果 EditText 为空,则必须禁用登录 Button.如果 EditText 有一些文本,则必须启用 Login 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.

两个字段都为空,登录按钮被禁用.

Both fields are empty, Sign in Button is DISABLED.

此处密码字段为空,因此登录按钮仍处于禁用状态.

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

这里的用户名和密码字段都不为空,所以登录按钮是启用的.

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天全站免登陆