自定义按钮颜色禁用和启用 [英] Custom button colours for disabled and enabled

查看:201
本文介绍了自定义按钮颜色禁用和启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了一个按钮,我想有黑色文本和背景,而它被禁用,然后一个绿色的背景,当它被启用白写一个Android应用程序。

香港专业教育学院得到了启用和禁用工作,但如果我改变颜色的按钮保持不变,无论其状态如何。

我读过,我需要使用自定义选择手动设置的颜色,这是我这么远:

在continuebutton.xml RES /绘(绿色是/res/values​​/colors.xml声明)

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目
机器人:state_enabled =假
    android.textColor =@机器人:彩色/黑白
    机器人:可绘制=@机器人:彩色/黑白/>
<项目
    机器人:state_enabled =真
    android.textColor =@机器人:彩色/白
    机器人:可绘制=@色/绿色/>

continuebutton布局xml文件:

 机器人:ID =@ + ID / continueButton
   机器人:layout_width =200dp
    机器人:layout_height =55dp机器人:layout_gravity =中心机器人:paddingTop =10dp机器人:TEXTSIZE =18dp
    机器人:文字=@字符串/ continueButton机器人:背景=@绘制/ continuebutton/>

我觉得我做得不对的continuebutton.xml文件,但我不知道如何解决它。

任何帮助将大大AP preciated!

感谢

更新:

我有背景变色,最后的问题是解决是下榻的​​黑色文本颜色不管按钮是否已禁用或启用(应该是白色启用)。

我是否需要在水库一个新的XML文件/绘制文本颜色?

任何想法?


解决方案

删除属性。

 的android:SRC =@绘制/ continuebutton

和使用

 的android:背景=@绘制/ continuebutton

在运行时可以通过

更改背景图片<$p$p><$c$c>myButton.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.myfirstbg));
myButton.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.mysecondbg));

如果你想使用的背景颜色删除这两个properties`

 的android:SRC =@绘制/ continuebutton
机器人:背景=@绘制/ continuebutton

和使用该更改背景颜色

  myButton.setBackgroundColor(Color.BLUE);
myButton.setBackgroundColor(Color.GREEN);

I have an android application that has a button which I want to have black text and background while it is disabled and then a green background with white writing when it is enabled.

Ive got the enabling and disabling working but if I change the colors the button stays the same regardless of its state.

I've read that I need to use a custom selector to set the colors manually and this is what I have got so far:

continuebutton.xml in res/drawable:(green is declared in /res/values/colors.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
android:state_enabled="false"
    android.textColor ="@android:color/black"
    android:drawable="@android:color/black" />
<item 
    android:state_enabled="true"
    android.textColor ="@android:color/white"
    android:drawable="@color/green" />

continuebutton in layout xml file:

 android:id="@+id/continueButton"
   android:layout_width="200dp"
    android:layout_height="55dp"  android:layout_gravity="center" android:paddingTop="10dp" android:textSize="18dp" 
    android:text="@string/continueButton" android:background="@drawable/continuebutton" />

I think I'm doing something wrong in the continuebutton.xml file but I'm not sure how to fix it.

Any help would be greatly appreciated!

Thanks

UPDATE:

I have the background changing color, the last problem is resolve is the text color which is staying black regardless of whether the button is disabled or enabled(should be white for enabled).

Do i need to make a new xml file in res/drawable for the text color?

Any ideas?

解决方案

Remove the attribute

android:src="@drawable/continuebutton"

and use

android:background="@drawable/continuebutton"

at runtime you can change the background image by

myButton.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.myfirstbg));
myButton.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.mysecondbg));

if you want to use background Color remove both properties`

android:src="@drawable/continuebutton"
android:background="@drawable/continuebutton"

and change background color by using this

myButton.setBackgroundColor(Color.BLUE);
myButton.setBackgroundColor(Color.GREEN);

这篇关于自定义按钮颜色禁用和启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆