如何设置不同的颜色按钮的pressed状态? [英] How do I set a different color for the pressed state of the button?

查看:228
本文介绍了如何设置不同的颜色按钮的pressed状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些按钮在我的Andr​​oid应用程序。他们有一个图标和文本。我可以设置在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:机器人=htt​​p://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屋!

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