Java 使textview处于选定状态(高亮显示)
因此,目前,我正在尝试为三个文本视图创建一个选定状态 目前,对于每个文本视图(H M S),文本为红色: 但是,当点击H M或S时,我希望它变成另一种颜色——白色 所以我试着这样做: 我这样做了(selected_text.xml):Java 使textview处于选定状态(高亮显示),java,android,xml,android-layout,Java,Android,Xml,Android Layout,因此,目前,我正在尝试为三个文本视图创建一个选定状态 目前,对于每个文本视图(H M S),文本为红色: 但是,当点击H M或S时,我希望它变成另一种颜色——白色 所以我试着这样做: 我这样做了(selected_text.xml): 并适用于: <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:te
并适用于:
<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" />
但是,文本视图在我单击它们后不会改变颜色
我该如何解决这个问题
而且,
我想知道在java代码中实现这一点是否更好,因为在单击/突出显示每个textview之后,我需要执行不同的函数。如果是,如何实施 您可以按如下方式编程:
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);
}
}
});
以下是我的布局:
<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>
检查这个主题是否有助于我理解,但我是问我是否按下它,然后它会保持不同的颜色,直到我再次按下它。@XiJiaopin这就是它的作用。它仅在重新创建活动时重置。是否有任何方法使其一次只选择一个?是否必须取消选择第一个以选择另一个?或者他们应该切换吗?假设选择了TextView A。触摸textview b时,textview A将自动取消选择,textview b是唯一选择的。
<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>
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);
}