如何使 setBackgroundColor 在按钮中的所有 android 版本上工作?

how to make the setBackgroundColor work on all android versions in a button?

我想在 ClickListener 中的按钮上设置背景颜色。我成功了,但我注意到它只适用于某些 android 版本。我用 phone 和 android 4.4 测试了它,但用 android 9 版本,它不起作用。这是我的代码和 build.gradle 文件内容:

 Button usd=findViewById(R.id.button_divide);
    Button cdf=findViewById(R.id.button_multiply);
    usd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences.Editor editor=sharedPref.edit();
            editor.putString("currency","USD");
            editor.apply();
            usd.setBackgroundColor(Color.parseColor("#515de1"));
            usd.setTextColor(Color.WHITE);
            cdf.setBackgroundColor(Color.WHITE);
            cdf.setTextColor(Color.parseColor("#515de1"));
        }

    });

此代码扩展 AppCompatActivity 和我的 build.gradle 文件:

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
    applicationId "com.example.e_mpatanfc"
    minSdkVersion 16
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

这是我的主题:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.EMpatanfc" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

和夜间版本

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.EMpatanfc" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_200</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_200</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

最后是 values/style.xml 内容:

   <?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="BottomNavigationView">
     <item name="android:textSize">24sp</item>

  </style>
   <style name="BottomNavigationView.Active">
    <item name="android:textSize">24sp</item>
    <item name="android:textColor">#515de1</item>
 </style>
</resources>

像这样使用 setBackgroundResource 而不是 setBackgroundColor

setBackgroundResource(R.color.red);

并在 colors.xml 文件中定义颜色

您可以使用方法 setBackgroundTintList:

usd.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.xxx));
cdf.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.xxx));

使用 Theme.MaterialComponents.* 主题,您的 Button 在运行时被替换为 MaterialButton。此方法适用于 min sdk>14.