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

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

问题描述

我想改变按钮内的文字颜色。



例如,我有一个按钮,用白色填充,文本是蓝色。当我点击按钮我想要这两种颜色交换(按钮内的文字变成白色和按钮蓝色)。



我试过这样: p>

 < item style =@ style / ButtonText_Menue_Clickedandroid:drawable =@ drawable / button_menue_defaultandroid:state_focused =true >< / item> 
< item style =@ style / ButtonText_Menueandroid:drawable =@ drawable / button_menue_default>< / item>

但实际上什么都不做。
有任何方法来做我想要的,或者我必须做一些东西在onclik事件(但是有问题如何设置颜色回来,当点击消失)

解决方案

res / color中创建选择器 / code>为 res / drawable 的文本颜色和按钮 selector / p>

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

 <?xml version =1.0encoding =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 =@ color / text_color
android:background =@ drawable / button_color/>


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>

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 a selector resource in res/color for the text color and a button selector in res/drawable as below...

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

<?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>

Add the button to the layout

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

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

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