无法正确显示对话框

时间:2017-10-12 06:22:39

标签: android android-alertdialog

我正在开发一个应用程序,我在按钮单击时从我的片段调用一个对话框但我的问题是,当我单击一个按钮然后出现对话框但它显示为暗淡的颜色。当我再次点击对话框时,它的背景变为白色。所以,我只想知道为什么会这样。

在按钮点击时从片段调用对话框的代码

       sms = (ImageView) v.findViewById(R.id.sms);
    sms.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (!admin_id.equals("0") && status.equals("A")) {
                DialogSMS dialogSMS = new DialogSMS(getActivity(), getContext());
                dialogSMS.show();
            } else {
                Toast.makeText(getActivity(), getResources().getString(R.string.disable_click), Toast.LENGTH_SHORT).show();

            }

        }

    });

对话框代码

    public class DialogSMS extends Dialog {

Context context;
ProgressDialog progressDialog;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
public static final String MY_PREF = "pref";
public static final String IS_SMS_SUBSCRIBE = "sms_status";
EditText etPostalCode;
EditText phoneNumber;
Dialog d;


public DialogSMS(Activity a, Context context) {
    super(a);
    this.context = context;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    progressDialog = new ProgressDialog(context);

    sharedPreferences = context.getSharedPreferences(MY_PREF, Context.MODE_PRIVATE);

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setMessage(R.string.app_sms_dialog_msg);
    alertDialog.setPositiveButton(R.string.app_close_dialog_msg_yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            SessionManager session = new SessionManager(context);

            if (session.isSmsSubscribed()) {

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
                alertDialog.setMessage(R.string.app_sms_unsubscribe);
                alertDialog.setPositiveButton(R.string.app_close_dialog_msg_yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        unSubscribeSMS();
                        dialog.cancel();
                    }
                });
                alertDialog.setNegativeButton(R.string.app_close_dialog_msg_no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                alertDialog.show();


            } else {
                d = new Dialog(getContext());
                d.setContentView(R.layout.dialog_select_sms);
                etPostalCode = (EditText) d.findViewById(R.id.etPostalCode);
                phoneNumber = (EditText) d.findViewById(R.id.phoneNumber);


                Button btnSend = (Button) d.findViewById(R.id.btnSend);
               d.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

                btnSend.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                        String mobile = phoneNumber.getText().toString();
                        String postalCode = etPostalCode.getText().toString();

                        validateFields(mobile, postalCode);

                        //subscribeSMS(postalCode, mobile);
                        // d.cancel();
                    }
                });
                d.show();
                dialog.cancel();

            }
        }
    });
    alertDialog.setNegativeButton(R.string.app_close_dialog_msg_no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    alertDialog.show();
}

现在首次出现对话框的屏幕截图链接

打开弹出窗口时的黑屏幕截图:

enter image description here

再次点击对话框时出现白色对话框:

enter image description here

2 个答案:

答案 0 :(得分:1)

  

我只是想知道为什么会这样发生

下面,

DialogSMS dialogSMS = new DialogSMS(getActivity(), getContext());
dialogSMS.show();

将显示一个对话框并在DialogSMS内部创建AlertDialog并再次显示,

AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setMessage(R.string.app_sms_dialog_msg);
--------------------------
alertDialog.show();

当您点击对话框时,它会取消对话框。

您实际上不需要在DialogSMS中创建AlertDialog。

如果您需要AlertDialog,那么为什么要显示Dialog?

*这可能对你有帮助 - * 只需将你的if-else条件带入

  

sms.setOnClickListener

并显示您的愿望AlertDialog

答案 1 :(得分:0)

使用dialog.cancel()代替@ECHO OFF PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~xyz0.ps1""' -Verb RunAs}" PAUSE

相关问题