Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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_Android Layout_Button_Text_Colors - Fatal编程技术网

Android 选择器没有';以编程方式更改后无法工作

Android 选择器没有';以编程方式更改后无法工作,android,android-layout,button,text,colors,Android,Android Layout,Button,Text,Colors,我有以下问题。我有一个不同颜色主题的应用程序。 在彩色布局的中间是一个带有白色角和白色文本的按钮。 按钮: <Button android:id="@+id/button" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_marginBottom="50dp"

我有以下问题。我有一个不同颜色主题的应用程序。 在彩色布局的中间是一个带有白色角和白色文本的按钮。 按钮:

<Button
            android:id="@+id/button"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="50dp"
            android:background="@drawable/selector_button"
            android:layout_gravity="center"
            android:text="Button"
            android:textAllCaps="false"
            android:textSize="18sp"
          android:textColor="@color/buttontextcolor_green"
            />
<selector
    <item
        android:state_focused="true"
        android:state_pressed="false"
        android:drawable="@drawable/button_off" />
    <item
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@drawable/button_on" />
    <item
        android:state_focused="false"
        android:state_pressed="true"
        android:drawable="@drawable/button_on" />
    <item
        android:drawable="@drawable/button_off" />
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item   android:state_focused="true"
        android:state_pressed="false"
        android:color="@color/md_white_1000" />

<item   android:state_focused="true"
        android:state_pressed="true"
        android:color="@color/greenPrimary"/>

<item   android:state_focused="false"
        android:state_pressed="true"
        android:color="@color/greenPrimary" />

<item   android:color="@color/md_white_1000" />
下次,当我按下按钮时,
textcolor
必须是蓝色,但它不是。
textcolor
在每次单击时都保持白色。只需在启动应用程序后第一次单击即可正常工作

我很抱歉,如果我的文字中有错误的话,我的德语会更好^^


问候语

使用
getColorStateList
。它会解决你的问题

更改行
button.setTextColor((getResources().getColor(R.color.buttonextcolor\u blue))

button.setTextColor(getResources().getColorStateList( R.color.buttonextcolor(蓝色))


并将buttonextcolor\u blue颜色放入颜色资源文件夹中,作为/res/color/buttonextcolor\u blue.xml

删除选择器并仅应用一种颜色
button.setTextColor((getResources().getColor(R.color.buttonextcolor\u blue))这就是为什么它不工作ButtonExtColor\u蓝色是一个xml文件,与ButtonExtColor\u绿色相同,只是项目属性中有其他颜色。但是我看到了问题,我不能使用颜色文件夹中的选择器?以前它位于drawable文件夹中,但由于在setTextColor方法中使用了drawable,因此我得到了一个编译器错误。只需使用getColorStateList
lBackground.setBackgroundColor(getResources().getColor(R.color.bluePrimary));
button.setTextColor((getResources().getColor(R.color.buttontextcolor_blue)));