Java 为 textview 设置选中状态(突出显示)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24798568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a selected state for textview (highlighted)
提问by Rohit Tigga
So currently, I am trying to create a selected state for three textviews
所以目前,我正在尝试为三个文本视图创建一个选定状态
Currently, for each of the textviews ( H M S) the text is red:
目前,对于每个文本视图(HMS),文本都是红色的:
However, when tap the H M or S I want it to turn another color--white.
然而,当点击 HM 或 SI 时希望它变成另一种颜色——白色。
So I tried to follow this:
所以我试图遵循这个:
Android - Textview change color on changing of state
and I did this (selected_text.xml):
我这样做了(selected_text.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="#ffd10011"/> <!-- selected -->
<item android:color="@color/red_highlight"/> <!-- default -->
</selector>
and applied that:
并应用:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Hours"
android:textSize="30sp"
android:textColor="@color/selected_text"
android:id="@+id/hourtext"
android:layout_marginLeft="45dp"
android:layout_alignTop="@+id/minutetext"
android:layout_alignLeft="@+id/seekArc"
android:layout_alignStart="@+id/seekArc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Minutes"
android:textSize="30dp"
android:textColor="@color/selected_text"
android:id="@+id/minutetext"
android:layout_below="@+id/seekArc"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Second"
android:textSize="30dp"
android:textColor="@color/selected_text"
android:id="@+id/secondtext"
android:layout_alignTop="@+id/minutetext"
android:layout_alignRight="@+id/seekArc"
android:layout_alignEnd="@+id/seekArc"
android:layout_marginRight="43dp" />
However, the textviews do not change color after I click on them.
但是,文本视图在我点击它们后不会改变颜色。
How do I fix this?
我该如何解决?
Also, I was wondering if it is better to implement this in java code since I need to perform a different function after each textview is clicked/highlighted. If so, how can that be implemented?
另外,我想知道在 Java 代码中实现它是否更好,因为我需要在单击/突出显示每个文本视图后执行不同的功能。如果是这样,如何实施?
采纳答案by Simas
You can do this programatically like this:
您可以像这样以编程方式执行此操作:
findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView = (TextView) v;
if (textView.isSelected()) {
textView.setTextColor(Color.RED);
v.setSelected(false);
} else {
textView.setTextColor(Color.BLUE);
v.setSelected(true);
}
}
});
And here's my layout:
这是我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text"
android:textSize="20sp"
android:textColor="#FF0000"/>
</LinearLayout>
EDIT:
编辑:
For your specific case would be like:
对于您的具体情况,如下所示:
View previousView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView previousText = (TextView) previousView;
TextView curText = (TextView) v;
// If the clicked view is selected, deselect it
if (curText.isSelected()) {
curText.setSelected(false);
curText.setTextColor(Color.RED);
} else { // If this isn't selected, deselect the previous one (if any)
if (previousText != null && previousText.isSelected()) {
previousText.setSelected(false);
previousText.setTextColor(Color.RED);
}
curText.setSelected(true);
curText.setTextColor(Color.BLUE);
previousView = v;
}
}
};
findViewById(R.id.txt).setOnClickListener(clickListener);
findViewById(R.id.txt2).setOnClickListener(clickListener);
findViewById(R.id.txt3).setOnClickListener(clickListener);
}