我正在尝试在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时,之前的文本颜色应该变回原始颜色,即白色。
答案 0 :(得分:0)
我会创建一个循环,它将遍历所有这些TextView。并根据其标签更改颜色。首先,我将所有TextViews放入一个ArrayList中,当点击它们中的任何一个时,我会调用一个方法来设置它们中的文本颜色。我将在几分钟内编写代码并发布。
public class MainActivity extends AppCompatActivity {
List<TextView> textViewList = new ArrayList<TextView>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView mGoal1 = (TextView)findViewById(R.id.goalText1); mGoal1.setTag("no");
TextView mGoal2 = (TextView)findViewById(R.id.goalText2);mGoal2.setTag("no");
TextView mGoal3 = (TextView)findViewById(R.id.goalText3);mGoal3.setTag("no");
textViewList.add(mGoal1);
textViewList.add(mGoal2);
textViewList.add(mGoal3);
mGoal1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.setTag("yes");
ChangeColor();
}
});
mGoal2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.setTag("yes");
ChangeColor();
}
});
mGoal3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.setTag("yes");
ChangeColor();
}
});
}
void ChangeColor(){
for (int i =0;i<textViewList.size();i++)
{
if (textViewList.get(i).getTag().equals("yes")){
textViewList.get(i).setTextColor(Color.parseColor("#000000"));
textViewList.get(i).setTag("no");
}
else textViewList.get(i).setTextColor(Color.parseColor("#ffffff"));
}
}
答案 1 :(得分:0)
要更改TextView
的颜色,您需要执行以下操作。
TextView.onClickListener() {
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView text = (TextView)view;
text.setTextColor(getResources().getColor(android.R.color.black));
}
});