2015-05-15 60 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(); 
     }});