如何在Android中将样式设置为禁用按钮?
我有一个按钮、样式和颜色状态文件:如何在Android中将样式设置为禁用按钮?,android,android-button,android-styles,material-components-android,Android,Android Button,Android Styles,Material Components Android,我有一个按钮、样式和颜色状态文件: <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button1" style="@style/btnGreen"
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1"
style="@style/btnGreen"/>
目标是:按钮启用=>小部件.MaterialComponents.Button的启用样式 禁用按钮=>Widget.MaterialComponents.Button.OutlinedButton的禁用样式 我需要这样的东西(伪代码):
我怎样才能做到这一点我不会以编程方式更改按钮属性,因为在不重新创建的情况下在白天和黑夜之间切换存在问题。谢谢。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" />
<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>
为什么不在按钮上使用
onClick()
来更改按钮的形状?您无法通过编程更改样式;您可以设置屏幕的外观、布局的一部分或XML中的单个按钮
from…您不能以这种方式更改样式,但可以更改所需的所有属性。我不会以编程方式更改按钮属性,因为在不重新创建的情况下在白天和黑夜之间切换存在问题。我不会只更改背景颜色。当按钮被禁用时,我想将样式从Widget.MaterialComponents.Button
更改为Widget.MaterialComponents.Button.OutlineButton
。此样式属性将对该按钮做什么?更改样式在XML文件中的标记中设置ONCLICK属性,如上图所示检查此链接,第二个链接将精确处理您的问题。
<selector>
<item style="Widget.MaterialComponents.Button.OutlinedButton" android:state_enabled="false"/>
</selector>
<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" />
<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;
}
}