【问题标题】:How to change the colour of Positive and negative button in Custom Alert dialog in android如何在android的自定义警报对话框中更改正负按钮的颜色
【发布时间】:2014-12-17 08:12:13
【问题描述】:

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

我要做什么:以及下面的代码,如何更改对话框中操作按钮的颜色(正面和负面)

@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();

【问题讨论】:

标签: android android-alertdialog


【解决方案1】:

你可以这样做-

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);
}

【讨论】:

  • 没有办法通过样式或主题来实现吗?对许多或每个对话都这样做似乎不是一个好主意
  • 是的,为什么不呢。你也可以通过样式来做到这一点。试试这个链接 -> stackoverflow.com/a/24560017/3912847
  • 我看不出该链接如何解决问题,哪些样式引用正按钮样式,哪些样式引用负按钮。我只看到android:borderlessButtonStyle
  • 您可以创建一个实用方法来创建 AlertDialog。每次需要对话框时,请使用该实用程序方法。没有代码重复。
  • 完美!帮助了我
【解决方案2】:
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>

【讨论】:

    【解决方案3】:

    您可以覆盖 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));
    }
    

    【讨论】:

      【解决方案4】:
      //***The simpliest solution is:***
      
      dialog.show(); //Only after .show() was called
      dialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(neededColor);
      dialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(neededColor);
      

      【讨论】:

        【解决方案5】:

        您可以使用 AlertDialog 的 setOnShowListener 方法来获取按钮并应用不同的颜色和它们自己的侦听器,这里有一个 Kotlin 中的示例:

        val alert : AlertDialog?
        val alertView = LayoutInflater.from(context).inflate(R.layout.alert_add_product, null, false)
        
        val builder = AlertDialog.Builder(context)
            builder.setCancelable(false)
            builder.setView(alertView)
        
            /**Create positive and negative buttons**/
            builder.setPositiveButton(android.R.string.ok, null)
            builder.setNegativeButton(android.R.string.cancel, null)
        
            alert = builder.create()
        
            /**Listener called when the AlertDialog is shown**/
            alert.setOnShowListener {
        
                 /**Get the positive button from the AlertDialog**/
                 val positiveButton = alert.getButton(DialogInterface.BUTTON_POSITIVE)
        
                 /**Set your color to the positive button**/
                 positiveButton.setTextColor(ContextCompat.getColor(context, R.color.bluePrimaryDark))
        
                 /**Set listener to the positive button**/
                 positiveButton.setOnClickListener({
                      alert.dismiss()
                 })
        
                 /**Get the negative button from the AlertDialog**/
                 val negativeButton = alert.getButton(DialogInterface.BUTTON_NEGATIVE)
        
                 /**Set your color to the negative button**/
                 negativeButton.setTextColor(ContextCompat.getColor(context, R.color.bluePrimaryDark))
        
                 /**Set listener to the negative button**/
                 negativeButton.setOnClickListener {
                      alert?.dismiss()
                 }
            }
        
            alert.show()
        

        【讨论】:

          猜你喜欢
          • 2020-06-21
          • 1970-01-01
          • 1970-01-01
          • 2018-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-05
          • 2020-07-06
          相关资源
          最近更新 更多