【发布时间】:2016-09-01 09:48:57
【问题描述】:
我正在尝试更改 Select 上 textView 中文本的文本颜色。我尝试了很多示例,但没有一个有效。 这是我的xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/goal"
android:layout_marginTop="15dp"
android:orientation="vertical">
<TextView
android:id="@+id/goalText1"
android:layout_width="match_parent"
android:layout_height="63dp"
android:background="@drawable/white_border_rounded"
android:padding="4dp"
android:layout_margin="18dp"
android:layout_gravity="fill"
android:gravity="center"
android:text="@string/goal1"
android:textColor="@color/white"
android:textSize="21sp"/>
<TextView
android:id="@+id/goalText2"
android:layout_width="match_parent"
android:layout_height="63dp"
android:background="@drawable/white_border_rounded"
android:padding="4dp"
android:layout_margin="18dp"
android:layout_gravity="fill"
android:gravity="center"
android:text="@string/goal2"
android:textColor="@color/white"
android:textSize="21sp"/>
<TextView
android:id="@+id/goalText3"
android:layout_width="match_parent"
android:layout_height="63dp"
android:background="@drawable/white_border_rounded"
android:padding="4dp"
android:layout_margin="18dp"
android:layout_gravity="fill"
android:gravity="center"
android:text="@string/goal3"
android:textColor="@color/white"
android:textSize="21sp"/>
<TextView
android:id="@+id/goalText4"
android:layout_width="match_parent"
android:layout_height="63dp"
android:background="@drawable/white_border_rounded"
android:padding="4dp"
android:layout_margin="18dp"
android:layout_gravity="fill"
android:gravity="center"
android:text="@string/goal4"
android:textColor="@color/white"
android:textSize="21sp"/>
</LinearLayout>
这是我的 white_border_rounded:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:state_selected="false" >
<shape android:shape="rectangle" >
<corners android:radius="50dip" />
<stroke android:width="1dip" android:color="@color/white" />
</shape>
</item>
<item android:state_pressed="true" android:state_selected="false" android:color="#444">
<shape android:shape="rectangle" >
<corners android:radius="50dip" />
<stroke android:width="1dip" android:color="@color/white" />
<solid android:color="#fff"/>
</shape>
</item>
<item android:state_pressed="false" android:state_selected="true" android:color="#444">
<shape android:shape="rectangle" >
<corners android:radius="50dip" />
<stroke android:width="1dip" android:color="@color/white" />
<solid android:color="#fff"/>
</shape>
</item>
<item >
<shape android:state_pressed="true" android:state_selected="true" android:color="#444">
<corners android:radius="50dip" />
<stroke android:width="1dip" android:color="@color/white" />
<solid android:color="#fff"/>
</shape>
</item>
下面是我的java文件:
public void onClick(View v) {
switch(v.getId()){
case R.id.goalText1:
changeViewBackground(true,false,false,false);
goal_selection = mGoal1.getText().toString();
break;
case R.id.goalText2:
changeViewBackground(false,true,false,false);
goal_selection = mGoal2.getText().toString();
break;
case R.id.goalText3:
changeViewBackground(false,false,true,false);
goal_selection = mGoal3.getText().toString();
break;
case R.id.goalText4:
changeViewBackground(false,false,false,true);
goal_selection = mGoal4.getText().toString();
break;
case R.id.btnGoal:
Intent intent = new Intent(this, fiteness_level_selection.class);
}
private void changeViewBackground(boolean view1,boolean view2,boolean
view3,boolean view4) {
mGoal1.setSelected(view1);
mGoal2.setSelected(view2);
mGoal3.setSelected(view3);
mGoal4.setSelected(view4);
}
我希望当用户选择任何 textView 时,其文本颜色应更改为黑色,而当它选择其他 textview 时,之前的文本颜色应更改回原始颜色,即白色。
【问题讨论】:
-
为其他人试试这个:- mGoal1.setBackgroundColor(getResources.getColor(R.color.Red));
-
@VishalPatoliya 我想更改文本颜色> 我也试过了,但这对我不起作用。
-
mGoal1.setTextColor(getResources.getColor(R.color.Red));
标签: android