在 styles.xml 中将对话框按钮更改为具有波纹效果的清除

Change Dialog Buttons to Clear with ripple effect in styles.xml

我有一个带有两个按钮的简单对话框,Positive 和 Negative,它采用我在 styles.xml 中定义的简单样式布局。我希望取消按钮有一个清晰的背景,但在触摸它时仍然有用户反馈(就像红色的涟漪效应)。我已经尝试了 2 天了,但没有成功。任何输入都会很棒。

使用涟漪效应:

我的下一个布局代码使布局具有清晰的取消背景但没有连锁反应。但我确实得到了 Yes 的连锁反应:

 <style name="AlertDialogThemeTesting" parent="Theme.AppCompat.Dialog.Alert">
    <item name="colorControlHighlight">@color/Red</item>
    <item name="colorControlActivated">@color/Red</item>
    <item name="colorControlNormal">@color/Red</item>
    <item name="android:textColor">@color/Black</item>
    <item name="android:textColorPrimary">@color/Gray</item>
    <item name="android:buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
    <item name="android:buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>

<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">@color/Black</item>
    <item name="android:background">?attr/selectableItemBackgroundBorderless</item>
    <item name="backgroundTint">@color/Clear</item>
</style>

<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">@color/Black</item>
    <item name="android:background">?attr/selectableItemBackgroundBorderless</item>
    <item name="android:backgroundTint">@color/Gold</item>
</style>

背景清晰但无波纹效果:

添加 Clear/White 背景似乎出于某种原因消除了涟漪效应。

对话代码:

final AlertDialog.Builder alert = new AlertDialog.Builder(PopOut.this,
        R.style.AlertDialogTheme);
alert   .setTitle("Delete Profile?")
        .setMessage("Are you sure you want to delete" + profile)
        .setPositiveButton("OK",
                new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog,
                                        int whichButton)
                    {
                        finish();
                        dialog.dismiss();
                    }
                })
        .setNegativeButton("Cancel", null);

final AlertDialog dialog = alert.create();
dialog.show();

定义您的 按钮,如下所示,

<LinearLayout

    android:id="@+id/test_btn"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="68dp"
    android:background="@drawable/button_effect"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="0dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:fontFamily="@font/titilliumweb_bold"
        android:gravity="center"
        android:text="Test Button"
        android:textColor="@color/UcabWhite" />

</LinearLayout>

在 drawable class 中创建 button_effect.xml,如下所示,

   <ripple xmlns:android="http://schemas.android.com/apk/res/android"
     android:color="@color/colorPrimary"     //colour you want the effect
     android:exitFadeDuration="@android:integer/config_shortAnimTime">
       <selector>
          <item>
             <color android:color="@android:color/transparent" />
          </item>
      </selector>
   </ripple>

同时在你的activity[=35=中为Button定义setOnClickListner ] 或 Dialog.

test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

我已经对此进行了测试,它非常有效。试一试,如果您有任何困难,请告诉我。

编辑

像下面这样定义您的警报对话框。当长按取消按钮时,您可以看到波纹效果。但是当你只是点击它时,没有时间显示效果,因为警报被取消了。

 final AlertDialog.Builder alert = new AlertDialog.Builder(this, R.style.AppTheme);
    alert.setTitle("Delete Profile?")
            .setMessage("Are you sure you want to delete")
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int whichButton) {
                            finish();
                            dialog.dismiss();
                        }
                    })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

    final AlertDialog dialog = alert.create();
    dialog.show();

    Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
    negativeButton.setBackgroundResource(R.drawable.button_mini_oval);

会有帮助。