禁用按钮单击动画,不带setEnable(false)

时间:2023-01-28 09:29:04

I have a LienarLayout with a Button in it. I have a OnClickListener on both the Button and the LinearLayout. At some point I would like to disable the Button and pass the onClick event to the parent LinearLayout. I found out that you achieve this by setting Button.setClickable(flase). Which works and the LinearLayout gets the click, however the Button's click animation is still played. Even worse, if I click on the LinearLayout where the Button is not drawn, the Buttons click animation still plays!

我有一个带有按钮的LienarLayout。我在Button和LinearLayout上都有一个OnClickListener。在某些时候,我想禁用Button并将onClick事件传递给父LinearLayout。我发现你通过设置Button.setClickable(flase)来实现这一点。哪个有效,而LinearLayout获得点击,但仍然会播放Button的点击动画。更糟糕的是,如果我点击未绘制按钮的LinearLayout,按钮点击动画仍会播放!

If anyone knows how I can achieve what I want, I would greatly appreciate it.

如果有人知道我怎么能达到我想要的,我会非常感激。

P.S.: The reason I don't want to use Button.setEnabled(false) is because I don't want the button to look disabled. I would also like to be able to enable / disable button's clickable state on demand. So basically I would like the button to be active sometimes and then other times for the click to pass through to the LinearLayout.

P.S。:我不想使用Button.setEnabled(false)的原因是因为我不希望按钮看起来被禁用。我还希望能够按需启用/禁用按钮的可点击状态。所以基本上我希望按钮有时处于活动状态,然后其他时间点击以传递给LinearLayout。

The code - xml:

代码 - xml:

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/selectableItemBackground">

    <Button
        android:id="@+id/button"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="button"/>

</LinearLayout>

The code - java:

代码 - java:

Button btn = (Button) view.findViewById(R.id.button);
btn.setClickable(false);

Before click: 禁用按钮单击动画,不带setEnable(false) During click on LinearLayout: 禁用按钮单击动画,不带setEnable(false)

点击之前:点击LinearLayout期间:

1 个解决方案

#1


2  

You can simply give background color so as the animation won't happen.

您可以简单地提供背景颜色,以便不会发生动画。

android:background="@android:color/white"

When you want clickable animation back, use this

如果您想要可点击的动画,请使用此功能

TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
yourButton.setBackgroundResource(outValue.resourceId);

This will solve this issue.

这将解决这个问题。

#1


2  

You can simply give background color so as the animation won't happen.

您可以简单地提供背景颜色,以便不会发生动画。

android:background="@android:color/white"

When you want clickable animation back, use this

如果您想要可点击的动画,请使用此功能

TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
yourButton.setBackgroundResource(outValue.resourceId);

This will solve this issue.

这将解决这个问题。