3

我想关闭 AlertDialog 窗口,但不是在单击“确定”或“取消”按钮时,而是在调用操作时。事实上,我想以这种方式在 onResume 函数中创建对话框:

@Override
public void onResume() {
    super.onResume();
    alert = new AlertDialog.Builder(this);
    alert.setTitle("Stay stuck to a tag");
    alert.create().show();
}

然后在调用函数时退出它,例如

private void dismissMyDiag() {
    alert.dismiss();
}

当然,我已经看到了通过 onclick 事件执行此操作的方法,这不是我想要做的。有可能做这样的事情吗?如果没有,什么对话框之王允许我这样做?

谢谢 !

4

3 回答 3

7

像这样获取alertDialog

AlertDialog dialog;
 alert = new AlertDialog.Builder(this);

dialog=alert.create().show();

然后打电话

private void dismissMyDiag() {
    dialog.dismiss();
}
于 2013-04-15T07:30:49.073 回答
1

试试这种方式:

final AlertDialog d = new AlertDialog.Builder(context)
                .setView(v)
                .setTitle(R.string.my_title)
                .setPositiveButton(android.R.string.ok,
                        new Dialog.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface d, int which) {
                                //Do nothing here. We override the onclick
                            }
                        })
                .setNegativeButton(android.R.string.cancel, null)
                .create();

        d.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {

                Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
                b.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // TODO Do something

                        //Dismiss once everything is OK.
                        d.dismiss();
                    }
                });
            }
        });
于 2013-04-15T07:30:53.447 回答
0

我知道这是个老问题,但可能希望这会对某人有所帮助。您还可以为警报对话框创建自定义类并访问它洞项目可以显示和隐藏任何地方

package (Your_Pakage_Name);
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatDialogFragment;


public class AlertDailogbox extends AppCompatDialogFragment {

    String Title = null;
    String Msg = null;
    String TAG = "Alert Dialog Box";

    public AlertDailogbox(String title, String msg) {
        Title = title;
        Msg = msg;
        
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(Title)
                .setMessage(Msg)
                .setIcon(R.drawable.ic_alert) //set icon if you want
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        
                    }
                });

        return builder.create();
    }
}

调用您的项目(活动)制作 alertDialog 类的全局变量

private AlertDailogbox alertDailogbox;

显示对话框的功能

private void showMsgDailog(String Title,String Msg){
    alertDailogbox = new AlertDailogbox(Title,Msg);
    alertDailogbox.setCancelable(false);
    alertDailogbox.show(getSupportFragmentManager(),"alert Dailog");
}

结束解雇

if(alertDailogbox != null)
       alertDailogbox.dismiss();

alerDailogeBox 不为空 检查是否必要 其他明智的应用程序可能会崩溃

于 2020-07-30T06:25:36.650 回答