警报对话框中的问题在肯定按钮验证中保持打开状态单击 android
issue in alert dialog remain open in Positive button validation click in android
我想在肯定按钮单击中打开我的警报对话框,我有 3 个验证条件我在下面检查它是我的代码
LayoutInflater li = LayoutInflater.from(getActivity());
View promptsView = li.inflate(R.layout.dialog_reset, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light_Dialog));
alertDialogBuilder.setView(promptsView);
final EditText mEditText = (EditText) promptsView
.findViewById(R.id.editText1);
final EditText mEditText2 = (EditText) promptsView
.findViewById(R.id.editText2);
final EditText mEditText3 = (EditText) promptsView
.findViewById(R.id.editText3);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("SUBMIT",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// get user input and set it to result
// edit text
mStringOldPass = mEditText.getText().toString().trim();
mStringNewPass = mEditText2.getText().toString().trim();
if (mEditText.getText().toString().trim()
.equalsIgnoreCase("")) {
mAllMethods.ShowDialog(getActivity(),
"Validation Error", "Enter Old Password",
"Ok");
} else if (mEditText2.getText().toString().trim()
.equalsIgnoreCase("")) {
mAllMethods.ShowDialog(getActivity(),
"Validation Error", "Enter New Password",
"Ok");
} else if (mEditText3.getText().toString().trim()
.equalsIgnoreCase("")) {
mAllMethods.ShowDialog(getActivity(),
"Validation Error",
"Enter Confirm Password", "Ok");
}
else if (!mEditText3
.getText()
.toString()
.trim()
.equalsIgnoreCase(
mEditText2.getText().toString().trim())) {
mAllMethods.ShowDialog(getActivity(),
"Validation Error",
"Enter new password not matching", "Ok");
} else {
if (mAllMethods.check_Internet() == true) {
mDialogPin.dismiss();
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
mEditText3.getWindowToken(), 0);
new changePass().execute();
} else {
mAllMethods.ShowDialog(getActivity(),
"Validation Error",
"Network not available", "Ok");
}
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
在上面的代码中,我检查了肯定按钮内的 3 个验证,所以我的验证工作正常,但我的警报对话框同时关闭,我想在我的验证条件触发时保持打开状态,所以知道我该如何解决它?
先创建监听器class
class CustomListener implements View.OnClickListener {
private final Dialog dialog;
public CustomListener(Dialog dialog) {
this.dialog = dialog;
}
@Override
public void onClick(View v) {
// Do whatever you want here
// If tou want to close the dialog, uncomment the line below
//dialog.dismiss();
}
}
并在显示对话框时写入
AlertDialog dialog = dialogBuilder.create();
dialog.show();
Button button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setOnClickListener(new CustomListener(dialog));
你也可以这样使用..
Button b = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "ok button is clicked");
}
});
我想在肯定按钮单击中打开我的警报对话框,我有 3 个验证条件我在下面检查它是我的代码
LayoutInflater li = LayoutInflater.from(getActivity());
View promptsView = li.inflate(R.layout.dialog_reset, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light_Dialog));
alertDialogBuilder.setView(promptsView);
final EditText mEditText = (EditText) promptsView
.findViewById(R.id.editText1);
final EditText mEditText2 = (EditText) promptsView
.findViewById(R.id.editText2);
final EditText mEditText3 = (EditText) promptsView
.findViewById(R.id.editText3);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("SUBMIT",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// get user input and set it to result
// edit text
mStringOldPass = mEditText.getText().toString().trim();
mStringNewPass = mEditText2.getText().toString().trim();
if (mEditText.getText().toString().trim()
.equalsIgnoreCase("")) {
mAllMethods.ShowDialog(getActivity(),
"Validation Error", "Enter Old Password",
"Ok");
} else if (mEditText2.getText().toString().trim()
.equalsIgnoreCase("")) {
mAllMethods.ShowDialog(getActivity(),
"Validation Error", "Enter New Password",
"Ok");
} else if (mEditText3.getText().toString().trim()
.equalsIgnoreCase("")) {
mAllMethods.ShowDialog(getActivity(),
"Validation Error",
"Enter Confirm Password", "Ok");
}
else if (!mEditText3
.getText()
.toString()
.trim()
.equalsIgnoreCase(
mEditText2.getText().toString().trim())) {
mAllMethods.ShowDialog(getActivity(),
"Validation Error",
"Enter new password not matching", "Ok");
} else {
if (mAllMethods.check_Internet() == true) {
mDialogPin.dismiss();
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
mEditText3.getWindowToken(), 0);
new changePass().execute();
} else {
mAllMethods.ShowDialog(getActivity(),
"Validation Error",
"Network not available", "Ok");
}
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
在上面的代码中,我检查了肯定按钮内的 3 个验证,所以我的验证工作正常,但我的警报对话框同时关闭,我想在我的验证条件触发时保持打开状态,所以知道我该如何解决它?
先创建监听器class
class CustomListener implements View.OnClickListener {
private final Dialog dialog;
public CustomListener(Dialog dialog) {
this.dialog = dialog;
}
@Override
public void onClick(View v) {
// Do whatever you want here
// If tou want to close the dialog, uncomment the line below
//dialog.dismiss();
}
}
并在显示对话框时写入
AlertDialog dialog = dialogBuilder.create();
dialog.show();
Button button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setOnClickListener(new CustomListener(dialog));
你也可以这样使用..
Button b = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "ok button is clicked");
}
});