如何使用自定义按钮关闭 AlertDialog.Builder

How to dismiss AlertDialog.Builder with custom button

我有自定义 AlertDialog,我想在用户单击 button 时关闭。

这是我的代码:

    Button btn = (Button) dialoglayout.findViewById(R.id.custom_alert_dialog_horarios_btn_aceptar);

    btn.setOnClickListener(new Button.OnClickListener() {

           @Override
           public void onClick(View arg0) {
            // TODO Auto-generated method stub
            //I want dismiss alertDialog

           }});


    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setView(dialoglayout);
    builder.show()

创建AlertDialog全局实例:

AlertDialog dialog;

dialog = builder.create();

使用对话框引用显示和关闭 AlertDialog :

dialog.show();
dialog.dismiss();

layout 内给 Dialog layout & Button 充气。为 button 注册一个 onClick listener

            LayoutInflater inflater = getActivity().getLayoutInflater();
            View dialoglayout=inflater.inflate(R.layout.your_layout, null); 
            Button button= (Button) dialoglayout.findViewById(R.id.custom_alert_dialog_horarios_btn_aceptar);

            button.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View arg0) {

              if(alert!=null&&alert.isShowing()){
               alert.dismiss();
               alert=null;

             }});

& 这是您 AlertDialogue

的代码
    final AlertDialog alert = new AlertDialog.Builder(new ContextThemeWrapper(context,android.R.style.Theme_Dialog)).create();
    alert.setTitle(title);
    alert.setMessage(message);
    alert.setIcon(R.drawable.warning_icon);
    alert.show();

不完全是问题的答案,但我使用 setPositiveButton 并使用 SetTextColor 和 setBackgroundColor 自定义解决了问题。

这是我的新代码:

        LayoutInflater inflater = getActivity().getLayoutInflater();
    View dialoglayout = inflater.inflate(R.layout.custom_alert_dialog_horarios, null);
    final TextView tv_texto = (TextView) dialoglayout.findViewById(R.id.custom_alert_dialog_horarios_texto);
    final TextView tv_titulo = (TextView) dialoglayout.findViewById(R.id.custom_alert_dialog_horarios_titulo);

    //Preparamos las fuentes personalizadas
    Typeface fontalertaTitulo = Typeface.createFromAsset(getActivity().getAssets(),"fonts/OpenSans-Semibold.ttf");
    Typeface fontalertaMensaje = Typeface.createFromAsset(getActivity().getAssets(),"fonts/OpenSans-Light.ttf");

    tv_titulo.setTypeface(fontalertaTitulo);
    tv_titulo.setText(getResources().getString(R.string.dias_de_cierre_alert_titulo));

    tv_texto.setTypeface(fontalertaMensaje);
    tv_texto.setText(getResources().getString(R.string.dias_de_cierre_texto));

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setPositiveButton(getResources().getString(R.string.aceptar), null);        
    builder.setView(dialoglayout);
    //builder.show();
    AlertDialog dialog = builder.create();
    dialog.show();

 // Customize the button
    Button button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
    button.setTextColor(getResources().getColor(color.donostiakirola_texto_general));
    button.setBackgroundColor(getResources().getColor(color.donostiakirola_fondo_pantalla));  
    //Preparamos las fuentes personalizadas
    Typeface fontTextoBoton = Typeface.createFromAsset(getActivity().getAssets(),"fonts/OpenSans-Semibold.ttf");
    button.setTypeface(fontTextoBoton);

你可以试试这个:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

builder.setView(dialoglayout);

final AlertDialog d = builder.show();

Button btn = (Button) dialoglayout.findViewById(R.id.custom_alert_dialog_horarios_btn_aceptar);

btn.setOnClickListener(new Button.OnClickListener() {

       @Override
       public void onClick(View arg0) {
          d.dismiss();
       }});