如何设置不同的颜色按钮的pressed状态? [英] How do I set a different color for the pressed state of the button?
问题描述
我有一些按钮
在我的Android应用程序。他们有一个图标和文本。我可以设置在java中code进行按钮
的背景颜色。如果点击该按钮,我想显示一个不同的颜色。那么,如何设置不同的颜色pssed的按钮的状态$ P $
?
<按钮
机器人:ID =@ + ID /保存
机器人:layout_width =130dip
机器人:layout_height =WRAP_CONTENT
机器人:scaleType =中心
机器人:drawableTop =@可绘制/保存
机器人:文本=保存
机器人:文字颜色=#FFFFFF
机器人:TEXTSIZE =14dip
>
在的onCreate
方法:
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.homescreen);
节省=(按钮)findViewById(R.id.save);
save.setBackgroundColor(Color.rgb(27161226)); }
使用按钮图像是这个样子mybutton.xml在绘制文件夹中创建XML文件
< XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目的android:STATE_ pressed =真正的机器人:可绘制=@色/绿彩/>
<项目安卓state_focused =真正的机器人:可绘制=@色/黄色颜色/>
<项目机器人:可绘制=@色/ redcolor/>
< /选择器>
在按钮XML code使用
机器人:背景:@绘制/ myButton的
添加这些颜色codeS中的资源 - >值 - > colors.xml像这样
< XML版本=1.0编码=UTF-8&GT?;
<资源>
<颜色名称=蓝色>#0066cc< /彩色>
<颜色名称=金与GT;#e6b121< /彩色>
< /资源>
参考:更改按钮的背景上触摸
I have some Buttons
on my android app. They have an icon and text. I can set the background color of a Button
in java code. If the button is clicked I want to display with a different color. So, how do I set a different color for the pressed state of the Button
?
<Button
android:id="@+id/save"
android:layout_width="130dip"
android:layout_height="wrap_content"
android:scaleType="center"
android:drawableTop="@drawable/save"
android:text="Save"
android:textColor="#FFFFFF"
android:textSize="14dip"
>
The onCreate
method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
save = (Button)findViewById(R.id.save);
save.setBackgroundColor(Color.rgb(27,161,226)); }
create xml file using the button image like this with mybutton.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/greencolor" />
<item android:state_focused="true" android:drawable="@color/yellowcolor" />
<item android:drawable="@color/redcolor" />
</selector>
and use this in button xml code
android:background:@drawable/mybutton
add those color codes in the resource-->values-->colors.xml like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#0066cc</color>
<color name="gold">#e6b121</color>
</resources>
Reference : Change button background on touch
这篇关于如何设置不同的颜色按钮的pressed状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!