RecyclerView适配器中的警报对话框

时间:2018-01-03 17:13:08

标签: java android alertdialog android-drawable

我已经实现了一个长时间点击监听器来从我的recyclerview中删除一个条目,我想给用户一个改变主意的最后机会。我在“标准”视图中使用警报对话框做了类似的事情,我为警报视图中的按钮做了一些绘图,我想再次使用(在我的所有视图中保持一个恒定的界面)< / p>

我正在使用的是:

<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/minus_button"
      android:state_pressed="false"
      android:state_selected="false"/>
  <item android:drawable="@drawable/add_button"
      android:state_pressed="true"/>

</selector>

我正在尝试使用的提醒是:

  public void deleteRequestCheck(final int tempIDval){

    AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
    builder.setMessage("Are you sure you want to reset all your climbs back to zero?");
    builder.setCancelable(false);
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
              deleteEntry(tempIDval);
        }

    });

    AlertDialog alert = builder.create();
    alert.show();
    Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));
    nbutton.setTextColor(Color.WHITE);
    Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
    pbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));
    pbutton.setTextColor(Color.WHITE);
}

我正在我的Adapter类中调用它(处理longclick动作)。

我遇到的问题是

 nbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));
 pbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));

给我一​​个错误Cannot resolve method 'getDrawable(int)'。这让我很难过,因为这与我以前的活动中创建和调用此警报对话框的方式相同。

1 个答案:

答案 0 :(得分:0)

实际上有两种方法可以做到这一点:

1)以编程方式:

nbutton.setBackground(ContextCompat.getDrawable(this.context‌​, R.drawable.mypositiveButtonDrawable));

但是如果你想要其他对话框的相同按钮颜色怎么办?为什么要重复这项工作?

2)通过styles.xml

在styles.xml中创建一个主题,如:

<style name="DiscardChangesDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="buttonBarNegativeButtonStyle">@style/DialogButtonNegative</item>
    <item name="buttonBarPositiveButtonStyle">@style/DialogButtonPositive</item>
</style>

<style name="DialogButtonNegative" parent="Widget.AppCompat.Button.Borderless">
    <item name="android:textColor">@color/dialog_button_negative</item>
</style>

<style name="DialogButtonPositive" parent="Widget.AppCompat.Button.Borderless">
    <item name="android:textColor">@color/dialog_button_positive</item>
</style>

然后像:

一样使用它
AlertDialog.Builder(this, R.style.DiscardChangesDialog)

然后你不需要担心适配器或任何东西。

希望它有所帮助!!!

相关问题