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

时间:2015-05-15 07:27:13

标签: android alertdialog

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

5 个答案:

答案 0 :(得分:4)

你可以试试这个:

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

答案 1 :(得分:1)

不完全是问题的答案,但我使用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);

答案 2 :(得分:0)

创建AlertDialog全局实例:

AlertDialog dialog;

dialog = builder.create();

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

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

答案 3 :(得分:0)

夸大你的Dialog layout& Button内的layout。为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

的代码
    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();

答案 4 :(得分:-2)

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
builder.setView(dialoglayout);   
Button btn = (Button)
dialoglayout.findViewById(R.id.custom_alert_dialog_horarios_btn_aceptar);

btn.setOnClickListener(new Button.OnClickListener() {

       @Override
       public void onClick(View arg0) {

      // dismiss your alertDialog here

       builder.dismiss();

      }});


builder.show();