2015-05-15 58 views
0

我有自定义AlertDialog,我想在用户单击button时解雇。如何使用自定义按钮取消AlertDialog.Builder

这是我的代码:

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

回答

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

创建AlertDialog全局实例:

AlertDialog dialog; 

dialog = builder.create(); 

使用对话框参考,以显示和罢免AlertDialog

dialog.show(); 
dialog.dismiss(); 
+0

对话框不能在OnClick方法 – aldakur

+0

必须在类级别声明AlertDialog对话框中reolved。 –

+0

NullPointerException – aldakur

0

虚增您Dialog layout & Button里面的layoutbutton注册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(); 
+0

语法erro on token,删除这些令牌。 '})。create();' – aldakur

+0

@aldakur对不起,语法错误。现在回答更新 –

+0

我没有找到语法错​​误 – aldakur

-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(); 
+0

你能解释一下你的代码吗?仅考虑代码的答案被认为质量很差(例如,参见[this](http://meta.stackexchange.com/q/148272))。 –

+2

builder.dismise()方法不存在 – aldakur

2

你可以试试这个:

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