【问题标题】:How can I set style to disabled button in Android?如何在 Android 中将样式设置为禁用按钮?
【发布时间】:2020-08-09 11:49:08
【问题描述】:

我有这个按钮、样式和颜色状态文件:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button1"
    style="@style/btnGreen"/>
<style name="btnGreen" parent="Widget.MaterialComponents.Button">
    <item name="android:textStyle">bold</item>
    <item name="backgroundTint">@color/color_states_btn_green</item>
</style>

颜色状态:color_states_btn_green.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#00FF00" android:state_enabled="true" />
    <item android:alpha="0.10" android:color="#00FF00"/>
</selector>

目标是:
启用按钮 => 启用 Widget.MaterialComponents.Button
样式 按钮禁用 => Widget.MaterialComponents.Button.OutlinedButton 的禁用样式

我需要这样的东西(伪代码):

<selector>
    <item style="Widget.MaterialComponents.Button.OutlinedButton" android:state_enabled="false"/>
</selector>

我怎样才能做到这一点? 我不会以编程方式更改按钮属性,因为在不重新创建的情况下在昼夜之间切换会出现问题。谢谢。

【问题讨论】:

  • 为什么不在按钮上使用onClick() 来改变按钮的形状?
  • you can't change styles programmatically; you can set the look of a screen, or part of a layout, or individual button in your XML 来自Christopher Orr...
  • 这种方式不能改变样式,但是你可以改变所有你需要的属性。
  • 我没有以编程方式更改按钮属性,因为在不重新创建的情况下在昼夜之间切换会出现问题。

标签: android android-button android-styles material-components-android


【解决方案1】:

XML:

<Button
            android:id="@+id/BSAMPLE"
            android:layout_width="60dp"
            android:layout_height="22dp"
            android:layout_gravity="center"
            android:layout_margin="3dp"
            android:background="#4B5E6E"
            android:gravity="center"
            android:padding="1dp"
            android:text="ENABLED"
            android:textColor="#ffffff"
            android:textSize="10sp"
            android:textStyle="bold"
            android:onClick="switchClicked"
            tools:layout_editor_absoluteX="300dp"
            tools:layout_editor_absoluteY="7dp" />

res>values>styles.xml:

<style name="btnGreen" parent="Widget.MaterialComponents.Button">
        <item name="android:textStyle">bold</item>
        <item name="backgroundTint">@color/colorPrimary</item>
    </style>

    <style name="Buttonenabled" parent="Widget.MaterialComponents.Button">
        <item name="android:background">@style/btnGreen</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:enabled">true</item>
        <item name="android:alpha">0.10</item>
    </style>

    <style name="ButtonDisabled" parent="Widget.MaterialComponents.Button.OutlinedButton">
        <item name="android:background">@style/btnGreen</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:enabled">true</item>
        <item name="android:alpha">0.10</item>
    </style>

然后按程序,

  public void switchClicked(View view) {
        boolean checked = ((Switch) view).isChecked();
        String str = "";

        switch (view.getId()) {
            case R.id.sw:
                if (checked){
                    view.setClickable(false);
                    view.setBackgroundColor(R.style.btnGreen);

                }
                else{
                    view.setClickable(false);
                    view.setBackgroundColor(R.style.btnGreen);
                   }
                break;
        }
    }


【讨论】:

  • 我不只改变背景颜色。当按钮被禁用时,我想将样式从 Widget.MaterialComponents.Button 更改为 Widget.MaterialComponents.Button.OutlinedButton
  • 这个样式属性会对那个按钮做什么?
  • 在 XML 文件中的
  • 检查此link 和此link 第二个链接正是您的问题。
猜你喜欢
  • 1970-01-01
  • 2011-05-28
  • 2014-11-22
  • 2011-03-07
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
相关资源
最近更新 更多