2012-01-14 35 views
5

是否可以自定義AlertDialog中的正負按鈕? 我需要用自定義替換正面和負面的默認外觀。可以自定義AlertDialog中的正負按鈕嗎?

.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {... 
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {... 

有人可以告訴我該怎麼做嗎?

+3

你必須創建一個自定義對話框。 :/這是我能找到的最簡單的教程。 http://i.thiyagaraaj.com/articles/android-articles/customdialogboxpopupusinglayoutinandroid – JustinDanielson 2012-01-14 10:57:48

回答

1

,如果你想改變他們,我會建議使用活動與你需要的佈局,並將它們添加=對話框中您的活動,在清單文件

1

,如果你想定製,你可以用它代替警告對話框 對話這裏是示例代碼

final Dialog dialog = new Dialog(ThisweekActivity.this, android.R.style.Theme_Translucent_NoTitleBar); 
    View view = LayoutInflater.from(ThisweekActivity.this).inflate(R.layout.issue_cover_prompt_layout, null); 
    view.findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 
    ImageView img = (ImageView) view.findViewById(R.id.issue_cover_img); 
    img.setImageBitmap(issue.getCoverImage()); 

    dialog.setContentView(view); 
    dialog.show(); 

可以設置在設置對話框中的佈局,並添加點擊聽者它

1

宥可以設置對話框中每個視圖。您可以使用兩個按鈕設置視圖,並且不設置正向&負向按鈕。

例如:

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

View dialogView = LayoutInflater.from(this) 
      .inflate(R.layout.my_layout, null); 

builder.setView(dialogView); 
+0

斷章取義! – 2016-10-30 22:26:51

+0

@Jivraj S Shekhawat好的,無論如何。如果你可以提供更好的答案,所以請做到這一點 – 2016-10-31 08:59:53

5
public class ComentarDialog extends DialogFragment{ 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

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

    builder.setMessage("Mensaje de alerta") 
      .setTitle("Comentar") 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

       } 
      }) 
      .setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

       } 
      }); 

    return builder.create(); 
} 

@Override 
public void onStart() { 
    super.onStart(); 

    //Personalizamos 

    Resources res = getResources(); 

    //Buttons 
    Button positive_button = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE); 
    positive_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog)); 

    Button negative_button = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE); 
    negative_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog)); 

    int color = Color.parseColor("#304f5a"); 

    //Title 
    int titleId = res.getIdentifier("alertTitle", "id", "android"); 
    View title = getDialog().findViewById(titleId); 
    if (title != null) { 
     ((TextView) title).setTextColor(color); 
    } 

    //Title divider 
    int titleDividerId = res.getIdentifier("titleDivider", "id", "android"); 
    View titleDivider = getDialog().findViewById(titleDividerId); 
    if (titleDivider != null) { 
     titleDivider.setBackgroundColor(color); 
    } 
} 
} 
+0

完美的答案。感謝分享。 :) – 2015-10-26 17:18:36

+0

FYI'button.setBackground()'...要求API級別16 – estoke 2016-02-08 16:13:32

相關問題