2012-01-14 99 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

相关问题