当前位置: 首页 > 知识库问答 >
问题:

如何在android的自定义警报对话框中更改正负按钮的颜色

邹普松
2023-03-14

我在做什么:我正在创建一个自定义的警告对话框

我想做的是:与下面的代码一起,如何更改对话框中动作按钮的颜色(正面和负面)

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
    // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // sign in the user ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });      
    return builder.create();

共有3个答案

云宜人
2023-03-14

您可以覆盖onStart方法来获得所有按钮的句柄(肯定、否定和中立)。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    View eventEditDialogView = View.inflate(this.getActivity(), R.layout.event_edit_dialog,
                                            null);

    builder.setTitle(getLocalizedString("edit_event"))
            .setView(eventEditDialogView)
            .setPositiveButton(getLocalizedString("all_events"), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                }
            })
            .setNegativeButton(getLocalizedString("this_event"), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            })
    return builder.create();
}

 @Override
    public void onStart() {
        super.onStart();
    Button positive = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
    positive.setTextColor(Color.BLACK);
    positive.setBackgroundColor(getResources().getColor(R.color.GrayBGColor));
}
廖绍辉
2023-03-14
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.DialogeTheme);
<style name="DialogeTheme" parent="Theme.AppCompat.Dialog.Alert">
    <item name="android:background">@color/all_item_bg</item>
    <item name="colorAccent">@android:color/white</item>
</style>
公孙茂学
2023-03-14

你可以这样做-

public void createDialog(final Context context) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage("Do you want to exit from app");
    builder.setCancelable(false);
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(context, "You exit from app",
                    Toast.LENGTH_LONG).show();

        }
    });

    AlertDialog alert = builder.create();
    alert.show();
    Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setBackgroundColor(Color.MAGENTA);
    Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
    pbutton.setBackgroundColor(Color.YELLOW);
}
 类似资料:
  • 我想改变颜色,想要一个更好的设计。。对此可以做些什么

  • 这里是我的创建对话框代码, 是否可以用蓝色而不是红色显示Disclose?

  • 我有一个弹出下载音频指令在我的应用程序。我正在尝试做的是,我想改变默认的文本颜色“OK”为蓝色。我试了一些东西,但不起作用。下面是我的代码: 但这种改变并没有反映出,有谁能告诉我,我做错了什么?

  • 我在硬件的后退按钮上有问题。在我的主要活动中,我有一个列表视图(例如1)。当我单击此列表视图(1)的项时,出现一个警报对话框,在此警报对话框中,有一个列表视图(例如2)。当我按下硬件的后退按钮时,这个列表视图(2)的数据正在重复。我还将取消图像放在这个警报对话框上,当我按下这个取消图像时,数据不会重复。我尝试了不同的方法onResume()、onPause()、onDestroy()、onRest

  • 我正在开发一个Android应用程序。我需要自定义警报对话框按钮,因为它以未指定的方式向我显示按钮。 调用警报对话框的代码为: 在

  • 我有一个简单的警报对话框与单选项目 我已经在我的主题中添加了自定义样式的警报对话框 这是我的风格 当我运行应用程序时,我会看到此警报 有没有办法在不创建自定义布局的情况下更改单选按钮附近文本的颜色?

  • 问题内容: 这是一个滑块难题。拼图完成后,我想显示一个带有“确定”按钮的对话框。当按下“确定”按钮时,我使用来通过Android浏览器加载网站。唯一的问题是,使用当前代码,当拼图完成后,它不会加载一个框(当我使用时会加载)。它什么也没做。有任何想法吗? 问题答案: 试试这个