我想使用样式更改颜色文本



这是我的TextView

<TextView
android:id="@+id/home_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Home"
android:textColor="@drawable/color_state"/>

这是我在可绘制文件夹中的color_state.xml代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:color="#fff"/>
<item
android:state_pressed="true"
android:state_selected="true"
android:color="#ff0000"/>
</selector>

我已经搜索过类似的问题,但我想要的是不同的东西,堆栈建议"可能重复"的所有问题,我已经尝试过,但这不是我想要的,这就是为什么我问这个问题以及我需要的所有解释。现有问题仅在单击TexttView时更改颜色 我在一个LinearLayout中有两个 TextView,我只想在单击它时更改文本的颜色,使用此代码我可以更改颜色,但是当我释放时单击颜色会恢复到原始颜色。我想要的只是在单击TextView时更改颜色,并且更改应该是永久性的,直到未单击另一个视图为止 请任何帮助将不胜感激

您可以使用 Enum 类轻松定义所需的颜色,这样,您就可以增加支持的颜色数量,而无需对代码进行太多更改。

枚举类

public enum ColorFactory {
BLACK(R.id.textView1, R.color.black),
WHITE(R.id.textView2, R.color.white),
BLUE(R.id.textView2, R.color.blue),
RED(R.id.textView2, R.color.red),
CYAN(R.id.textView2, R.color.cyan),
GREEN(R.id.textView2, R.color.green),
GREY(R.id.textView2, R.color.grey),
YELLOW(R.id.textView2, R.color.yellow),
PINK(R.id.textView2, R.color.pink),
PURPLE(R.id.textView2, R.color.purple),
BROWN(R.id.textView2, R.color.brown),
CUSTOM(R.id.textView2, R.color.custom);
int id;
int color;
ColorFactory(int id, int color) {
this.id = id;
this.color = color;
}
public int getId() {
return id;
}
public int getColor() {
return color;
}

public static int getColorById(int id) {
for (ColorFactory currentEnumElement : values()) {
if (currentEnumElement.getId() == id) {
return currentEnumElement.getColor();
}
}
return -1;
}
}

您必须在colors.xml文件中定义这些颜色,如下所示

颜色.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="black">#000000</color>
<color name="white">#ffffff</color>
<color name="blue">#0000ff</color>
<color name="red">#ff0000</color>
<color name="cyan">#00f2ff</color>
<color name="green">#00ff00</color>
<color name="grey">#8b8378</color>
<color name="yellow">#ffd700</color>
<color name="pink">#e8a4a3</color>
<color name="purple">#b72bef</color>
<color name="brown">#a7823e</color>
<color name="custom">#990000</color>
</resources>

到最后,使用它的代码...(简体(

法典

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView textView1;
TextView textView2;
.
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = . . . .
.
.
.
.
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.textView1:
textView1.setTextColor(ColorFactory.getColorById(view.getId()));
break;
case R.id.textView2:
.
.
.
}
}
}

它应该允许您设置作为单击结果的颜色。 注意:您还可以使用 ButterKnife 绑定视图以提高代码的可读性等。 希望对您有所帮助。

如果要使文本视图颜色在单击时永久更改,并在单击另一个文本视图时恢复正常。你需要像这样设置java代码。

TextView home = (TextView) findViewById(R.id.home_text);
TextView another = (TextView) findViewById(R.id.another_text);
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
home.setTextColor(Color.GREEN);
another.setTextColor(Color.BLACK);
}
});
another.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
home.setTextColor(Color.BLACK);
another.setTextColor(Color.RED);
}
});

最新更新