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

oyt4ldly  于 2022-11-03  发布在  Android
关注(0)|答案(5)|浏览(160)

我有自定义的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()
eaf3rand

eaf3rand1#

您可以尝试以下操作:

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();
       }});
5fjcxozz

5fjcxozz2#

不完全是这个问题的答案,但我修复了这个问题使用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);
1hdlvixo

1hdlvixo3#

创建AlertDialog全局示例:

AlertDialog dialog;

dialog = builder.create();

使用对话框参考显示和关闭AlertDialog

dialog.show();
dialog.dismiss();
57hvy0tb

57hvy0tb4#

layout内为您的Dialog layoutButton充气。注册一个onClick listener即可获得button

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的程式码(& H)

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();
8ehkhllq

8ehkhllq5#

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

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

builder.setView(dialoglayout);
AlertDialog alert = builder.create();
btn.setOnClickListener(new Button.OnClickListener() {

       @Override
       public void onClick(View arg0) {
          alert.cancel();
       }});
alert.show();

相关问题