简体   繁体   中英

setTextAppearance - not work for MaterialButton when try to disable click

Android 6.0

Android Studio 3.6

in my fragment when click button then I change style "on fly" like this:

  bluetoothPageViewModel.isDisableModeLiveData().observe(this, Observer {
      dataBinding.buttonStartSearchBluetooth.setTextAppearance(
          R.style.buttonDisableStyle
      );
  })

here xml layout:

 <com.google.android.material.button.MaterialButton
     android:id="@+id/buttonStartSearchBluetooth"
     style="@style/buttonStyle"
     android:layout_width="0dp"
     android:layout_height="@dimen/button_height"
     android:layout_margin="@dimen/button_margin"
     android:onClick="onClickButtonStartSearch"
     android:text="@string/start_search"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent" />

and here styles.xml

<style name="buttonDisableStyle" parent="@style/Widget.MaterialComponents.Button">
    <item name="android:textColor">@color/default_button_textColor</item>
    <item name="backgroundTint">@color/button_bg_color</item>
    <item name="android:enabled">false</item>
    <item name="android:clickable">false</item>
    <item name="android:textAppearance">@style/byttonTexAppearanceStyle</item>
</style>

<style name="byttonTexAppearanceStyle" parent="@style/TextAppearance.MaterialComponents.Button">
    <item name="android:textSize">18sp</item>
    <item name="android:textAllCaps">true</item>
</style>

But after call setTextAppearance I can still click button. Why?

setTextAppearance only take care about styling the Text of the View. Check the official doc to see supported property that setTextAppearance affected.

You have to handle the other property manually, Check below:

bluetoothPageViewModel.isDisableModeLiveData().observe(this, Observer {
    dataBinding.buttonStartSearchBluetooth.setTextAppearance(
        R.style.byttonTexAppearanceStyle
    );

    dataBinding.buttonStartSearchBluetooth.setEnabled(false);
    dataBinding.buttonStartSearchBluetooth.setClickable(false);
    dataBinding.buttonStartSearchBluetooth.setBackgroundTintList(
        ContextCompat.getColorStateList(this, R.color.button_bg_color))
    );
})

<style name="byttonTexAppearanceStyle" parent="@style/TextAppearance.MaterialComponents.Button">
    <item name="android:textSize">18sp</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:textColor">@color/default_button_textColor</item>
</style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM