在Android中管理EditText状态

时间:2013-02-15 11:58:29

标签: android android-edittext

您好我有一个登录表单,其中有两个EditText,一个用于电子邮件,一个用于密码,一个登录按钮,当用户按下按钮我想要对表单进行一些验证。例如,如果未输入到EditText中,则更改提示颜色和EditText的文本,这是迄今为止我尝试过的内容。我得到它所以它注销为“”但它没有改变提示。

Editable emailInputEditable = emailInput.getText();
    String emailInputString = emailInputEditable.toString();
    Editable passwordInputEditable = passwordInput.getText();
    String passwordInputString = passwordInputEditable.toString();

    Log.v("loginForm", "emailInputString = " + emailInputString);
    Log.v("loginForm", "passwordInputString = " + passwordInputString);

    if(emailInputString ==""){emailInput.setHint("Please enter your email");}
    if(passwordInputString ==""){passwordInput.setHint("Please enter your password");}

4 个答案:

答案 0 :(得分:2)

使用此

if(emailInputString.length==0){emailInput.setHint("Please enter your email");}
    if(passwordInputString.length==0){passwordInput.setHint("Please enter your password");}

要禁用登录按钮,请使用以下代码

if(emailInputString.length==0 && passwordInputString.length==0)
signbtn.setVisibility(View.GONE);

答案 1 :(得分:0)

试试这个:

if(emailInputString.equals("")){emailInput.setHint("Please enter your email");}
if(passwordInputString.equals("")){passwordInput.setHint("Please enter your password");}

您将此代码放入按钮点击事件

答案 2 :(得分:0)

比较字符串使用equals() ...

编辑。太晚了:/

答案 3 :(得分:0)

试试这个..

    if(!TextUtils.isEmpty(emailInput.getText()){

            if(!TextUtils.isEmpty(passwordInput.getText())){

                // Perfom your logic here..

            }else
                passwordInput.setError("Please enter your password");

        }else
            emailInput.setError("Please enter your email..");

快乐编码:)