你好我做了一个像
这样的活动....
<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中有一个提示,如果我点击登录按钮,那么它应该显示吐司需要密码或用户名。但它不是:(。第二次使用
if (userName != null && !userName.trim().equals("") && password != null && !password.trim().equals("")) {
dialog.show();
callWebServide(userName, password);
}
只有在userName和password不能为空时才显示“对话框”,但只要我点击“登录”按钮然后对话框开始显示,只有提示位于editText中。我在点击dialog.show()
时没有提及。虽然我正在创建一个对话框,但没有显示它。
为什么表现出乎意料?我做错了吗?
由于
答案 0 :(得分: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);
}