可以在AlertDialog中自定义正负按钮吗? 我需要用自定义替换正面和负面的默认外观。
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {...
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {...
有人可以告诉我该怎么做吗?
答案 0 :(得分: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);
}
}
}
答案 1 :(得分:1)
如果你想改变它们,我建议你使用你所需布局的活动,并在你的活动中添加它们=对话框,在清单文件中
答案 2 :(得分: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();
您可以在对话框中设置布局并在其上添加点击列表器
答案 3 :(得分:1)
Yuo可以设置对话框中的每个视图。你可以用两个按钮设置视图,不要设置正面和&负面按钮。
示例:
AlertDialog.Builder builder =
new AlertDialog.Builder(this);
View dialogView = LayoutInflater.from(this)
.inflate(R.layout.my_layout, null);
builder.setView(dialogView);