我有一个按钮,如下所示.当我想禁用它时my_btn.setEnabled(false),我会使用它,但我也想将它变灰.我怎样才能做到这一点?
谢谢
<Button android:id="@+id/buy_btn" style="@style/srp_button" />
Run Code Online (Sandbox Code Playgroud)
风格/ srp_button
<style name="srp_button" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/btn_default</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">14sp</item>
<item name="android:typeface">serif</item>
<item name="android:paddingLeft">30dp</item>
<item name="android:paddingRight">30dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
绘制/ btn_default.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/pink" />
<corners android:radius="6dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
Sia*_*ash 154
您还可以通过设置alpha(使其半透明)使其显示为已禁用.如果您的按钮背景是图像,并且您不想为其创建状态,则此功能尤其有用.
button.setAlpha(.5f);
button.setClickable(false);
Run Code Online (Sandbox Code Playgroud)
Adi*_*mro 55
你必须在你btn_defaut.xml的选择器中提供3或4个状态.
您将相应地为州提供效果和背景.
这里有一个详细的讨论:标准的Android按钮具有不同的颜色
小智 51
最简单的解决方案是将滤色器设置为按钮的背景图像,如我在此处所见
你可以这样做:
if ('need to set button disable')
button.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
else
button.getBackground().setColorFilter(null);
Run Code Online (Sandbox Code Playgroud)
希望我能帮助别人......
Ali*_*azi 15
所有给出的答案都可以正常工作,但我记得学习使用setAlpha可能是一个糟糕的主意表现(这里有更多信息).因此,创建StateListDrawable是管理禁用按钮状态的更好主意.这是如何做:
在res/drawable文件夹中创建XML btn_blue.xml:
<!-- Disable background -->
<item android:state_enabled="false"
android:color="@color/md_blue_200"/>
<!-- Enabled background -->
<item android:color="@color/md_blue_500"/>
Run Code Online (Sandbox Code Playgroud)
在res/values/styles.xml中创建按钮样式
<style name="BlueButton" parent="ThemeOverlay.AppCompat">
<item name="colorButtonNormal">@drawable/btn_blue</item>
<item name="android:textColor">@color/md_white_1000</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后将此样式应用于您的按钮:
<Button
android:id="@+id/my_disabled_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/BlueButton"/>
Run Code Online (Sandbox Code Playgroud)
现在当你打电话给btnBlue.setEnabled(true)OR时btnBlue.setEnabled(false),状态颜色会自动切换.
Sat*_*aty 11
将Clickable设置为false并将背景颜色更改为:
callButton.setClickable(false);
callButton.setBackgroundColor(Color.parseColor("#808080"));
Run Code Online (Sandbox Code Playgroud)
小智 11
您应该为禁用按钮创建XML文件(drawable/btn_disable.xml)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/grey" />
<corners android:radius="6dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
并为按钮创建一个选择器(drawable/btn_selector.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_disable" android:state_enabled="false"/>
<item android:drawable="@drawable/btn_default" android:state_enabled="true"/>
<item android:drawable="@drawable/btn_default" android:state_pressed="false" />
</selector>
Run Code Online (Sandbox Code Playgroud)
将选择器添加到按钮
<style name="srp_button" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/btn_selector</item>
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
121296 次 |
| 最近记录: |