收听已禁用的AlertDialog-Button的点击次数

时间:2016-04-19 16:11:11

标签: android android-button android-dialog

我想听取positive AlertDialog按钮的点击次数,我已通过拨打button.setEnabled(false);禁用了该按钮。

我该怎么做?如果无法做到这一点,是否有已知的解决方法?

PS。我想这样做的原因是,当有人按下按钮时,我想要表示敬酒,说“你需要在继续之前做到这一点。”

1 个答案:

答案 0 :(得分:0)

这不是监听已禁用按钮的点击次数的方法。这是一种解决方法。

我喜欢改变按钮颜色的结果,让它看起来像是被禁用了。

你想做什么:

// 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.
  }

现在,不要忘记按钮动作,只要它被禁用"

public void onClick(View v) {
    if (buttonDisabled) {
        // Button is clicked while it's disabled
    } else {
        // Button is clicked while it's enabled, like normal
    }
}

应该这样做,玩得开心。