如何启动从AlertDialog活动? [英] How to start an activity from an AlertDialog?
本文介绍了如何启动从AlertDialog活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个 AlertDialog
:
public class ConfirmChoixDownloadDialogView extends AlertDialog {
private Activity activity; // this activity shows this Dialog
private View contenu, titleBar;
private final int OUI = DialogInterface.BUTTON_POSITIVE;
private final int NON = DialogInterface.BUTTON_NEGATIVE;
private final int NEUTRE = DialogInterface.BUTTON_NEUTRAL;
public static final int requestCode = 100;
private int btnClicked = NEUTRE;
private AsyncTask<String, Void, Void> indirectTask;
@SuppressLint("InlinedApi")
public ConfirmChoixDownloadDialogView(Context context, LayoutInflater inflater) {
super(context, AlertDialog.THEME_HOLO_DARK);
contenu = inflater.inflate(R.layout.msg_dialog, null);
((TextView)contenu.findViewById(R.id.msgText)).setText(context.getResources().getString(R.string.questionChargementWebVersMobile));
titleBar = inflater.inflate(R.layout.custom_dialog_title, null);
((ImageView)titleBar.findViewById(R.id.icone)).setImageDrawable(context.getResources().getDrawable(R.drawable.ic_action_about));
((TextView)titleBar.findViewById(R.id.titre)).setText(context.getResources().getString(R.string.titreConfirmMsgBox));
setCustomTitle(titleBar);
setView(contenu, 0, 0, 0, 0);
setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.button_oui), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
btnClicked = OUI;
dialog.dismiss();
}
});
setButton(DialogInterface.BUTTON_NEGATIVE, context.getResources().getString(R.string.button_non), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
btnClicked = NON;
dialog.dismiss();
}
});
setOnDismissListener(dismissListener);
}
public void afficher(Activity act, AsyncTask<String, Void, Void> tacheIndirecte) {
activity = act;
indirectTask = tacheIndirecte;
show();
}
private OnDismissListener dismissListener = new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (dialog instanceof ConfirmChoixDownloadDialogView) {
if (btnClicked == OUI) {
indirectTask.execute("commune.txt",
"parcelles.txt",
"rues.txt",
"batiments.txt",
"appartements.txt",
"occupants_appartement.txt",
"categories_activite.txt",
"natures_activite.txt",
"activites_dans_appart.txt",
"contribuables.txt");
}
else if (btnClicked == NON) {
Intent i = new Intent(activity, SelectionQuartierActivity.class);
activity.startActivity(i); // it makes app crash
}
}
}
};
}
正如你可以看到我想要的情况下启动一个活动
在 BUTTON_NEGATIVE
的对话框
点击。但它崩溃!因此,如何在这种情况下启动活动
?
As you can see I want to start an activity
in the case when the BUTTON_NEGATIVE
of the Dialog
is clicked. But it crash ! So how to start the activity
in this case ?
推荐答案
好吧,我发现一个提示,启动活动
从活动
由此拉开了对话框
通过执行活动的
并设置 onWindowFocusChanged
方法活动的
中的公共静态
变量 OnDismissListener
的对话框
!
Ok , I found a tip to start the activity
from the activity
which opened the Dialog
by implementing the onWindowFocusChanged
method of the activity
and setting a public static
variable of the activity
inside the OnDismissListener
of the Dialog
!
这篇关于如何启动从AlertDialog活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文