android开发分享如何变灰一个button?

我有一个button,如下所示。 当我想禁用它,我使用my_btn.setEnabled(false) ,但我也想灰色。 我怎样才能做到这一点?

谢谢

 <Button android:id="@+id/buy_btn" style="@style/srp_button" /> 

风格/ 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> 

绘制/ btn_default.xml

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android"> <solid android:color="@color/pink" /> <corners android:radius="6dp" /> </shape> 

    您必须在您的btn_defaut.xml提供3个或4个状态作为select器。

    您将相应地为国家提供效果和背景。

    这里有一个详细的讨论: 不同颜色的标准Androidbutton

    你也可以使它显示为禁用购买设置alpha(使其半透明)。 如果你的button背景是一个图像,并且你不想为它创build状态,这是特别有用的。

     button.setAlpha(.5f); button.setClickable(false); 

    最简单的解决scheme是将滤色器设置为button的背景图像,如我在这里看到的

    你可以做如下:

     if ('need to set button disable') button.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY); else button.getBackground().setColorFilter(null); 

    希望我帮助了一个人

     Button button = (Button)findViewById(R.id.buy_btn); button.setEnabled(false); 

    将可点击设置为false并将背景颜色更改为:

     callButton.setClickable(false); callButton.setBackgroundColor(Color.parseColor("#808080")); 

    所有给定的答案工作正常,但我记得学习使用setAlpha可能是一个坏主意性能明智(更多信息在这里 )。 所以创build一个StateListDrawable是一个更好的办法来pipe理button的禁用状态。 就是这样:

    在res / drawable文件夹中创build一个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"/> 

    在res / values / styles.xml中创build一个button样式

     <style name="BlueButton" parent="ThemeOverlay.AppCompat"> <item name="colorButtonNormal">@drawable/btn_blue</item> <item name="android:textColor">@color/md_white_1000</item> </style> 

    然后将此样式应用于您的button:

     <Button android:id="@+id/my_disabled_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/BlueButton"/> 

    现在当你调用btnBlue.setEnabled(true)btnBlue.setEnabled(false) ,状态颜色会自动切换。

    你应该为禁用的button创build一个XML文件( drawable / btn_disable.xml

     <shape xmlns:android="https://schemas.android.com/apk/res/android"> <solid android:color="@color/grey" /> <corners android:radius="6dp" /> </shape> 

    并为button创build一个select器( drawable / btn_selector.xml

     <selector xmlns:android="https://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> 

    将select器添加到您的button

     <style name="srp_button" parent="@android:style/Widget.Button"> <item name="android:background">@drawable/btn_selector</item> </style> 

    你可以做一个灰色的图片,并把你的绘图,并将其添加到您的代码。

     button.setBackgroundResource(R.drawable.grey); 

    容易,简单,但由于使用图片它获得更多的空间。

      以上就是android开发分享如何变灰一个button?相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/515640.html

      (0)
      上一篇 2020年12月1日
      下一篇 2020年12月1日

      精彩推荐