简体   繁体   English

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

[英]How to dismiss AlertDialog.Builder with custom button

I have Custom AlertDialog and i want dismiss when the user click the button . 我有Custom AlertDialog ,我想在用户点击button时解雇。

This is my code: 这是我的代码:

    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()

You can try this : 你可以试试这个:

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

Not exactly the answer to the question but i fix the problem using setPositiveButton and custom with SetTextColor and setBackgroundColor. 不完全是问题的答案,但我使用setPositiveButton和自定义与SetTextColor和setBackgroundColor修复问题。

This is my new code: 这是我的新代码:

        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);

Create AlertDialog global instance : 创建AlertDialog全局实例:

AlertDialog dialog;

dialog = builder.create();

Use dialog reference to show and dismiss AlertDialog : 使用对话框参考来显示和关闭AlertDialog

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

Inflate your Dialog layout & Button inside the layout . 虚增您的Dialog layoutButton的内部layout Register a onClick listener for button . button注册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;

             }});

& Here is the code for your AlertDialogue 这是您的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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM