监听禁用的 AlertDialog-Button 上的点击

Listening for clicks on a disabled AlertDialog-Button

我想监听对 AlertDialogpositive 按钮的点击,我已通过调用 button.setEnabled(false);.

禁用了该按钮

我应该怎么做?如果这不可能,是否有已知的解决方法?

PS。我想这样做的原因是,我想在有人按下按钮时祝酒,说 "You need to do this before you can continue"。

这不是一种侦听对已禁用按钮的点击的方法。这是一个解决方法。

我喜欢通过更改按钮的颜色获得的结果,使它看起来像被禁用了。

您想做什么:

// Instantiate positive button
  final Button posButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);

// Save the original button's background
  final Drawable bg = posButton.getBackground();

// Set button's looks based on boolean
  if (buttonDisabled) {
      posButton.setTextColor(getResources().getColor(R.color.disabledButtonColor, null));
         // R.color.disabledButtonColor == #DBDBDB, which is pretty close to 
         //    the color a disabled button gets.
      posButton.setBackgroundColor(Color.TRANSPARENT);
         // Color.TRANSPARENT makes sure all effects the button usually shows disappear.
  } else {
      posButton.setTextColor(getResources().getColor(R.color.colorPrimaryDark, null));
         // R.color.colorPrimaryDark is the color that gets used all around my app.
         //    It was the closest to the original for me.
      posButton.setBackground(bg);
         // bg is the background we got from the original button before.
         //    Setting it here also re-instates the effects the button should have.
  }

现在,不要忘记在 "disabled"

时捕捉您的按钮操作
public void onClick(View v) {
    if (buttonDisabled) {
        // Button is clicked while it's disabled
    } else {
        // Button is clicked while it's enabled, like normal
    }
}

应该可以,玩得开心。