0

嗨,我做了一个活动,比如

....
<TableRow >
    <TextView
        android:text="@string/userName"
        android:width ="100dp" />

    <EditText
        android:id="@+id/txt_userName"
        android:width="100dp"
        android:hint="@string/userName" />
</TableRow>

<TableRow>
    <TextView
        android:text="@string/password" />

    <EditText
        android:id="@+id/txt_password"
        android:inputType="textPassword"
        android:hint="@string/password" />
</TableRow>
    ....

这是我的课

public class LoginActivity extends Activity {
    private boolean rememberpassword = false;
    ProgressDialog dialog = null;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        Button btn_logIn = (Button)findViewById(R.id.btn_signIn );
        btn_logIn.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View view) {
                showProgressDialog();
                getUserCredentials();           
            }           
        }); //end of anonymous class
    } //end of onCreate

    private void showProgressDialog() {

        dialog = new ProgressDialog(this);
        dialog.setMessage("Please Wait. Your authentication is in progress");
        dialog.setButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {                
                dialog.dismiss();               
            }
        }); //end of anonymous class    
    } //end of showProgressDialog()

    private void getUserCredentials() {

        EditText txt_userName = (EditText) findViewById(R.id.txt_userName);
        String userName = txt_userName.getText().toString();

        EditText txt_password = (EditText) findViewById(R.id.txt_password);
        String password = txt_password.getText().toString();

        if (userName != null && !userName.trim().equals("") && password != null && !password.trim().equals("")) {           
            dialog.show();
            callWebServide(userName, password);         
        } else if (userName == null) {          
            Toast.makeText(this, "User Name is required", Toast.LENGTH_LONG).show();        
        } else if (password == null && password.trim().equals("")) {        
            Toast.makeText(this, "Password is required", Toast.LENGTH_LONG).show();         
        }   
    } //end of getUserCredentials()

} //end of class LoginActivity

首先我的两个条件都不起作用

} else if (userName == null) {
    Toast.makeText(this, "User Name is required", Toast.LENGTH_LONG).show();        
} else if (password == null && password.trim().equals("")) {
    Toast.makeText(this, "Password is required", Toast.LENGTH_LONG).show();         
}

意味着当我的活动启动时,editText 中有一个提示,如果我点击登录按钮,那么它应该显示需要密码或用户名的 toast。但它不是:(。第二个我用过

if (userName != null && !userName.trim().equals("") && password != null && !password.trim().equals("")) {       
    dialog.show();
    callWebServide(userName, password);         
}

仅当用户名和密码不能为空时才应显示对话框,但是一旦我单击登录按钮,对话框就会开始显示,只有提示在 editText 中。我没有在我的点击中提到dialog.show()。虽然我正在创建一个对话框但没有显示它。

为什么它的行为出乎意料?我做错什么了吗?

谢谢

4

1 回答 1

0

检查应该是

} else if (userName == null || userName.lenght()==0) {
    Toast.makeText(this, "User Name is required", Toast.LENGTH_LONG).show();        
} else if (password == null || password.trim().equals("")) {
    Toast.makeText(this, "Password is required", Toast.LENGTH_LONG).show();         
}

为了这

不确定这个但测试一次:

if ( (userName != null) && (!(userName.lenght()>0)) && (password != null) && (!(password .lenght()>0))) {       
    dialog.show();
    callWebServide(userName, password);         
}
于 2012-06-28T15:09:18.707 回答