【问题标题】:Can't get onClick() call back after setStateListAnimator(null)在 setStateListAnimator(null) 后无法获得 onClick() 回调
【发布时间】:2023-03-28 08:06:02
【问题描述】:

标题是不言自明的,我有一个按钮,我称之为setStateListAnimator(null);。我还注册了OnClickListener 回调。问题是每当我打电话给setStateListAnimator(null) 时,我都没有收到onClick() 的回调。

<Button android:id="@+id/urlSubmittButton"
    android:layout_below="@id/editLyout"
    android:layout_width="150dp"
    android:layout_height="45dp"
    android:text="Add"
    android:layout_marginTop="10dp"
    android:textSize="18sp"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:padding="5dp" />

在我的Fragment:

urlSubmitButton = (Button) mView.findViewById(R.id.urlSubmittButton);

urlSubmitButton.setClickable(true);
urlSubmitButton.setEnabled(true);
urlSubmitButton.setOnClickListener(this);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    urlSubmitButton.setStateListAnimator(null);
}

如您所见,我尝试设置 Button 可点击并启用,它不会改变行为。然而,简单地评论那行代码就很有魅力。

这是 API 24 的预期行为吗?因为到目前为止,我只能在 API 24 设备中重现此问题。如果没有,那么是否有任何其他方法可以停止stateListAnimator,从而在单击时在Button 中产生水滴涟漪效应?

【问题讨论】:

    标签: android android-view android-button android-7.0-nougat rippledrawable


    【解决方案1】:

    如果没有,那么是否有任何其他方法可以停止 stateListAnimator,从而在单击时在 Button 中产生水滴涟漪效应?

    首先,我不认为stateListAnimator 与涟漪效应有某种联系。 Ripple 只是设置为Button 的背景,而stateListAnimator 根据视图的当前状态指定要执行的动画。查看StateListDrawable 中的状态。

    为了完全隐藏涟漪效应,将此应用于您的活动样式:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      ...
      <item name="selectableItemBackground">@null</item>
      <item name="selectableItemBackgroundBorderless">@null</item>
    </style>
    

    【讨论】:

    • 嗯,我认为Button 中的android:animation 使用state_pressed 设置为涟漪效应(我当然可能错了)。你仍然没有提到为什么不管 stateListAnimator actually 做什么都会停止onClick() 回调。但是,我会检查style 并让您知道它是否有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 2010-12-05
    • 1970-01-01
    • 2018-04-23
    相关资源
    最近更新 更多