当我在不输入值的情况下单击保存按钮时,对话框会消失。我想显示对话框并进行验证。如果有人知道,请帮助我。
这是我的代码:
TextView tvUserName=new TextView(this);
TextView tvPassword=new TextView(this);
TextView tvURL=new TextView(this);
final EditText etUserName=new EditText(this);
final EditText etPassword=new EditText(this);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
LinearLayout login= new LinearLayout(this);
login.setOrientation(1); //1 is for vertical orientation
tvUserName.setText(getResources().getString(R.string.username));
tvPassword.setText(getResources().getString(R.string.password));
login.addView(tvURL);
login.addView(etURL);
login.addView(tvUserName);
login.addView(etUserName);
login.addView(tvPassword);
etPassword.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);
login.addView(etPassword);
alert.setView(login);
alert.setTitle(getResources().getString(R.string.login));
alert.setPositiveButton(getResources().getString(R.string.login), new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, int whichButton) {
strUserName=etUserName.getText().toString();
XmlUtil.username=strUserName;
strPassword=etPassword.getText().toString();
strhwdXml=etURL.getText().toString();
if((strUserName.length()==0)||(strPassword.length()==0){
Toast.makeText(getBaseContext(),"Please enter username and password", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getBaseContext(),"Success", Toast.LENGTH_SHORT).show();
}