启用和禁用时无法更改按钮背景

3
这是我选择的背景:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@drawable/button_background" android:state_enabled="true" />
    <item android:drawable="@drawable/button_disable_background" android:state_enabled="false" />
</selector>

启用后台

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:endColor="#2F98E4"
        android:startColor="#2F98E4"
        android:type="linear" />
    <corners android:radius="20dp"></corners>

</shape>

禁用背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:endColor="#2F98E4"
        android:startColor="#10344E"
        android:type="linear" />
    <corners android:radius="20dp"></corners>

</shape>

按钮样式

 <style name="ButtonStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:textSize">20sp</item>
        <item name="android:gravity">center</item>
        <item name="android:background">@drawable/button_background_selector</item>
        <item name="android:padding">16dp</item>
    </style>

按钮代码
 <TextView
        android:id="@+id/btn_enter_pin_click"
        style="@style/ButtonStyle"
        android:layout_marginTop="@dimen/dp_108"
        android:layout_marginBottom="@dimen/dp_44"
        android:text="@string/done"
        android:enabled="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_enter_pin_preview" />

以下是我尝试启用和禁用按钮时的代码,但我无法设置按钮的背景,不知道哪里出了问题。

当我运行这段代码时,我得到了以下错误信息:

由于 android.view.InflateException: Binary XML file line #11: Binary XML file line #113: Error inflating class TextView 引起:android.view.InflateException: Binary XML file line #113: Error inflating class TextView 引起:android.content.res.Resources$NotFoundException: Drawable com.anil.gorestapp:drawable/button_background_selector with resource ID #0x7f0800d9 引起:android.content.res.Resources$NotFoundException: File res/drawable/button_background_selector.xml from drawable resource ID #0x7f0800d9

请帮助我解决这个问题。

1个回答

0

有更好的方法来完成这个。注意到你的两种样式除了背景之外完全相同。相反,创建一个StateListDrawable背景,指定启用和禁用状态下的背景。然后,您只需将其设置为背景,并使用setEnabled启用或禁用按钮,它会自动为您切换。


你能帮我吗?我实际上无法设置背景选择,我尝试过了,但是没有成功。 - MARSH
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@drawable/button_background" android:state_enabled="true"/> <item android:drawable="@drawable/button_disable_background" android:state_enabled="false"/> <item android:drawable="@drawable/button_background" android:state_enabled="false"/> </selector>。 (注意:这是已经翻译好的文本,不需要再进行翻译。) - MARSH
你那里有两个false,其中一个是错误的背景。它应该只有两个项目,一个为启用,一个为禁用。 - Gabe Sechan
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@drawable/button_background" android:state_enabled="true" /> <item android:drawable="@drawable/button_disable_background" android:state_enabled="false" /> </selector> 即使保留了两个项目,它仍然无法工作。我得到了“Error inflating class TextView”的错误。 - MARSH
我已经修改了问题,你能帮我吗? - MARSH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接