2017-10-12 120 views
0

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

代码从按钮片段调用对话框中,单击

 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

回答

0

使用dialog.dismiss()而不是dialog.cancel()

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,那么你为什么要显示对话框?

*这可以帮助你 - * 只要把你的if-else条件内

sms.setOnClickListener

,并显示你的愿望AlertDialog