在我的应用中,我正在使用带有自定义视图的AlertDialog
。此视图包含几个EditText
和其他一些内容。 EditText
附加了一些侦听器,可以执行所需的任何功能。
在按钮上单击对话框,我正在对输入EditText
的数据执行一些验证,如果数据无效,则显示另一个AlertDialog
。关闭该(第二个)对话框后,我想保留在主对话框中(使用自定义视图),但出于某种原因,在显示第二个对话框之前关闭对话框。如何在第二个对话框下保持原始(自定义视图)对话框保持打开状态?
这是我的(简化)代码:
final EntryPanel panel = new EntryPanel(OrderActivity.this);
AlertDialog dlg = new AlertDialog.Builder(OrderActivity.this)
.setCancelable(true)
.setView(panel)
.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final int id = pePanel.getProductId();
if(!isValidProduct(id)) {
new AlertDialog.Builder(OrderActivity.this)
.setMessage(R.string.error_unknown_product)
.setCancelable(true)
.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dlg2, int which) {
dlg2.cancel();
}
})
.show();
}
else {
processProductEntry(id);
dialog.dismiss();
}
}
})
.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
答案 0 :(得分:1)
因此,不可能保持两个对话框都打开。因此,我将第一个对话框(使用自定义视图)更改为theme
为Theme.Dialog
的活动 - 它显示应该的方式 - 然后从中打开第二个对话框。这样,当第二个对话框打开时,原始活动(看起来像一个对话框)仍然在后台可见。
答案 1 :(得分:0)
我遇到了类似的问题,我就这样解决了这个问题:
public class PayDebtsDialogFragment extends DialogFragment implements OnClickListener
{
public interface OnDebtPayListener{
void debtPayed(BalanceItem debt, double payedAmount);
void badPayment(int errorCode);
}
private static final String LOG_TAG = "PayDebtsDialogFragment";
private static final int BAD_PARSING = -1;
private OnDebtPayListener onDebtPayListener;
private BalanceItem debtPayment;
private EditText etAmount;
public void setOnDebtPayListener(OnDebtPayListener onDebtPayListener) {
this.onDebtPayListener = onDebtPayListener;
}
public BalanceItem getDebtPayment() {
return debtPayment;
}
public void setDebtPayment(BalanceItem debtPayment) {
this.debtPayment = debtPayment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(getDialogLayout())
.setTitle("Pay debt")
.setNegativeButton("cancel", this)
.setPositiveButton("pay", this);
return builder.create();
}
@Override
public void onClick(DialogInterface dialog, int which)
{
if(which == DialogInterface.BUTTON_POSITIVE){
if(onDebtPayListener != null){
try {
String strPayment = etAmount.getText().toString().trim();
double payment = Double.valueOf(strPayment);
Log.d(LOG_TAG, "debtPayed()");
onDebtPayListener.debtPayed(debtPayment, payment);
} catch (Exception e) {
Log.d(LOG_TAG, "badPayment()", e);
onDebtPayListener.badPayment(BAD_PARSING);
}
}else{
Log.e(LOG_TAG, "onDebtPayListener is NULL");
}
}
dismiss();
}
private View getDialogLayout()
{
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.pay_debts_dialog_fragment, null);
TextView tvPayer = (TextView) dialogLayout.findViewById(R.id.tvPayDebts_payer);
tvPayer.setText(debtPayment.getPayer().getName() + " pays");
etAmount = (EditText) dialogLayout.findViewById(R.id.etPayDebts_Amount);
String hint = String.format(Locale.getDefault(), "Total debt: %.2f%s", debtPayment.getAmount(),
debtPayment.getCurrency());
etAmount.setHint(hint);
TextView tvReceiver = (TextView) dialogLayout.findViewById(R.id.tvPayDebts_Receiver);
tvReceiver.setText("to " + debtPayment.getReceiver().getName());
return dialogLayout;
}
}
答案 2 :(得分:0)
我已经遇到了同样的问题并经过了大量搜索,但没有发现任何相关问题。
所以,我只是在那里使用了一点点黑客来达到我的要求。
首先我宣布AlertDialogs:
AlertDialog.Builder subDialog,mainDialog;
然后像这样使用hack:
subDialog = new AlertDialog.Builder(Activity2.this)
.setMessage("New Dialog Opened")
.setCancelable(true)
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dlg2, int which) {
dlg2.cancel();
mainDialog.show();
}
});
mainDialog = new AlertDialog.Builder(Activity2.this)
.setCancelable(true)
.setMessage("First Dialog Opened")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
subDialog.show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
mainDialog.show();
我做的是对二次打开的子对话的ok动作,我再次为MainDialog调用show动作。
希望它会对你有所帮助。
感谢。