Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:点击改变字体颜色_Android_Button_Text_Colors - Fatal编程技术网

Android 安卓:点击改变字体颜色

Android 安卓:点击改变字体颜色,android,button,text,colors,Android,Button,Text,Colors,我正在尝试更改按钮内文本的颜色 例如,我有一个按钮,它是用白色填充的,文本是蓝色的。当我点击按钮时,我希望这两种颜色互换(按钮内的文本变为白色,按钮变为蓝色) 我试过这样的方法: <item style="@style/ButtonText_Menue_Clicked" android:drawable="@drawable/button_menue_default" android:state_focused="true"></item> <item styl

我正在尝试更改按钮内文本的颜色

例如,我有一个按钮,它是用白色填充的,文本是蓝色的。当我点击按钮时,我希望这两种颜色互换(按钮内的文本变为白色,按钮变为蓝色)

我试过这样的方法:

 <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事件中做一些事情(但是当“点击”消失时如何设置颜色存在问题)

res/color
中为文本颜色创建一个
选择器
资源,在
res/drawable
中创建一个按钮
选择器
,如下所示

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>
<?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_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>
<?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="@color/text_color"
    android:background="@drawable/button_color"/>


据我所知,您需要在onClick中完成。非常简单的代码。我怎么把它改回来呢?还有一个问题,就是当你完成点击时,文本会改变。但是我想在我松开按钮之前更改按钮文本(释放按钮后调用onclick),您可以使用,并将其设置为我以前没有使用过的文本颜色。我会调查一下,看看它是否能帮我做我想做的事:)谢谢你的回答:)太好了,这正是我要找的,thankstext_color.xml是一个,所以我认为把它放在
color
目录比放在
drawable
目录更好,因为将drawable设置为
textColor
会让人困惑。