安卓:更改点击字体颜色 [英] Android: change font color on click

查看:229
本文介绍了安卓:更改点击字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变内部的按钮文本的颜色。

i am trying to change the color of text inside a button.

因此​​,举例来说,我有一个充满白色按钮和文本是蓝色的。当我按一下按钮,我想这两种颜色互换(文字里面的按钮变成白色和蓝色的按钮)。

So for example i have a button which is filled with white and the text is blue. When i click on the button i want these two colors to swap (text inside the button becomes white and the button blue).

我已经试过这样的事情:

I have tried something like this:

 <item style="@style/ButtonText_Menue_Clicked" android:drawable="@drawable/button_menue_default" android:state_focused="true"></item>
 <item style="@style/ButtonText_Menue" android:drawable="@drawable/button_menue_default" ></item>

但它实际上什么也不做。 有没有办法做我想要的,或者做我必须做一些东西在onclik事件(但有问题如何设置的颜色回来的时候点击不见了)

but it actually does nothing. Is there any way to do what i want, or do i have to do some stuff in the onclik event (but then there is the problem how to set the colors back when the "click is gone" )

推荐答案

创建选择 绘制文本和按钮色并设置为按钮如下...

Create selector drawable for text and button color and set to button as below...

text_color.xml

text_color.xml

<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_pressed="true"
    android:color="#800000" />
<item
    android:state_pressed="false"
    android:color="#4C5" />
 </selector>

button_color.xml

button_color.xml

 <?xml version="1.0" encoding="utf-8"?>
 <selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_pressed="true">
    <shape android:shape="rectangle">
        <solid android:color="#4C5"/>
    </shape>
    </item>
<item
    android:state_pressed="false">
    <shape android:shape="rectangle"  >
        <solid android:color="#800000"/>
    </shape>
</item>
</selector>

添加按钮布局

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:textColor="@drawable/text_color"
    android:background="@drawable/button_color"/>

这篇关于安卓:更改点击字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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