Disable login button till form is filled android sdk

隐身守侯 提交于 2019-12-12 00:47:36

问题


Hello all simple question how do you disable the submit button till to edittext are filled is it an if command or something ??

    b = (Button)findViewById(R.id.login);  
    et = (EditText)findViewById(R.id.username);
    pass= (EditText)findViewById(R.id.password);

    if (et & pass == '  ') {

    }

    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog = ProgressDialog.show(Login.this, "", 
                    "Validating user...", true);
             new Thread(new Runnable() {
                    public void run() {
                        login();                          
                    }
                  }).start();               
        }
    });

回答1:


What you might want to do is use a TextWatcher and in onTextChanged() check to see if each is empty. But you want to check the text not the View so instead of checking et and pass, you probably want to get the String in each.

String etString = et.getText().toString();
String passString = pass.getText().toString();

If they are not emty and null, plus any other checks you want such as length then

b.setEnabled(true);



回答2:


Maybe this isn't what you're looking for, but the

b.setEnabled(false);

will disable the button for you, then you can enable it when you need it:

b.setEnabled(true);




回答3:


To make sure the username is not empty we get text from the EditText and trim() it to remove newlines,spaces and tabs from beginning and end of the string. Then we check whether the length is greater than 0. We make the same check for the password and then enable the button functionality using setEnabled(true).

b = (Button)findViewById(R.id.login);  
et = (EditText)findViewById(R.id.username);
pass= (EditText)findViewById(R.id.password);
b.setEnabled(false); 
if (et.getText().toString().trim().length() > 0 && 
            pass.getText().toString().length() > 0){
    b.setEnabled(true);
}

b.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog = ProgressDialog.show(Login.this, "", 
                "Validating user...", true);
         new Thread(new Runnable() {
                public void run() {
                    login();                          
                }
              }).start();               
    }
});


来源:https://stackoverflow.com/questions/16115708/disable-login-button-till-form-is-filled-android-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!