如何在两个EditText上一起处理addTextChangedListener?

时间:2015-03-11 16:32:57

标签: android layout android-edittext actionlistener

要求:

有两个EditText一个是用户名,另一个是密码。 我正在检查两个EditText有多个字符而不是注册按钮应该启用。

任何人都可以建议我吗?

etUsername.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) {
                int userName = s.length();
                if(userName >=1){
                    btnSignup.setEnabled(true);
                }

            }
        });
        etPswd = (EditText) findViewById(R.id.etpswd);
        etPswd.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) {
                int pswd = s.length();
                if(pswd >=1) {
                    btnsignup.setEnabled(true);
                }

            }
        });

etUsername.setOnKeyListener(this); 
etPswd.setOnKeyListener(this); 
private void trydisableLoginButton() 
{ 
if(etUsername.getText().toString().length() == 0 && etPswd.getText().toString().length() == 0)
{ btnLogin.setEnabled(false); 
} 
} 

@Override public boolean onKey(View v, int keyCode, KeyEvent event) 
{ 
if(keyCode == KeyEvent.KEYCODE_BACK) 
{
    trydisableLoginButton(); 
} 
   return false;
 }

所需物品。我正在尝试禁用返回按下softKeyboard上的按钮 上面的代码不起作用!

1 个答案:

答案 0 :(得分:0)

创建一个启用注册按钮的功能

 public void tryEnableSignUpButton(){

    if(etUsername.getText().toString().lenght>=1 && (etPswd .getText().toString().lenght>=1 )){

 btnSignup.setEnabled(true);
}

}

现在每个

之后
            @Override
            public void afterTextChanged(Editable s) {
                int userName = s.length();
                if(userName >=1){
                   tryEnableSignUpButton();
                }

            }

<强> @EDIT

@Override
    public void onBackPressed() {
      trydisableLoginButton(); 
      super.onBackPressed(); 
}