Android:以编程方式更改为实心xml形状会使文本消失

Android:以编程方式更改为实心xml形状会使文本消失,android,xml,Android,Xml,我的版面中有一个按钮,其背景和文本颜色被定义为选择器。未按下按钮时,按钮具有背景颜色和文本颜色,按下按钮时,按钮具有另一种颜色和文本颜色-例如,白色背景与黑色文本->黑色背景与白色文本 在代码中的某一点上,我需要用第三组颜色替换背景/文本颜色,然后返回xml中定义的原始选择器。然而,返回后,文本不再显示,取而代之的是我只得到一个纯色按钮 <Button android:id="@+id/somebutton" android:layout_width="80" android:layout

我的版面中有一个按钮,其背景和文本颜色被定义为选择器。未按下按钮时,按钮具有背景颜色和文本颜色,按下按钮时,按钮具有另一种颜色和文本颜色-例如,白色背景与黑色文本->黑色背景与白色文本

在代码中的某一点上,我需要用第三组颜色替换背景/文本颜色,然后返回xml中定义的原始选择器。然而,返回后,文本不再显示,取而代之的是我只得到一个纯色按钮

<Button
android:id="@+id/somebutton"
android:layout_width="80"
android:layout_height="80"
android:textColor="@color/sometext_selector"
android:background="@drawable/somebackground_selector"
            android:gravity="center"
            android:text="@string/sometext"
            android:textSize="15sp"
            />
然后这个,

someButton.setBackgroundDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.originalBackgroundSelectorDefinedInXml, null));

someButton.setTextColor(getResources().getColor(R.color.originalTextSelectorDefinedInXml));
问题是什么时候开始的

以下是我的选择器的XML-为伪代码道歉,请想象适当的十六进制值:

第一,背景:

<item android:drawable="@drawable/color_purple_full" android:state_selected="true"/>
<item android:drawable="@drawable/color_purple_full" android:state_pressed="true"/>
<item android:drawable="@drawable/color_purple_full" android:state_focused="true"/>
<item android:drawable="@drawable/color_purple_outline"/>

颜色\紫色\轮廓:

android:shape="oval">
<stroke android:width="2dp" android:color="#somepurplehexa" />
<size android:width="80dp" android:height="80dp"/> </shape>
android:shape=“oval”>
颜色\紫色\完整:

android:shape="oval">
<solid android:color="#somepurplehexa"/>
<size android:width="80dp" android:height="80dp" /> </shape>
android:shape=“oval”>
正文:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:color="@android:color/black" android:state_selected="true"/>
<item android:color="@android:color/black" android:state_pressed="true"/>
<item android:color="@android:color/black" android:state_focused="true"/>
<item android:color="@color/green"/>

我看到的是在未按下状态下的正确背景,但在按下状态下,我得到的只是一个纯色,上面没有任何文字。如果您能告诉我为什么会发生这种情况,以及为什么在原始xml中它工作得很好,但只是在以编程方式更改后才停止工作,我将不胜感激

已解决:

原来问题出在这里:

someButton.setTextColor(getResources().getColor(R.color.originalTextSelectorDefinedInXml));
应该是

someButton.setTextColor(getResources().getColorStateList(R.color.originalTextSelectorDefinedInXml));
someButton.setTextColor(getResources().getColorStateList(R.color.originalTextSelectorDefinedInXml));