【问题标题】:Make a selected state for textview (highlighted)为 textview 设置选定状态(突出显示)
【发布时间】:2014-09-08 00:45:05
【问题描述】:

所以目前,我正在尝试为三个文本视图创建一个选定状态

目前,对于每个文本视图 (H M S),文本都是红色的:

但是,当点击 H M 或 S 时,我希望它变成另一种颜色——白色。

所以我试着遵循这个:

Android - Textview change color on changing of state

我做了这个(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>

并应用:

  <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代码中实现这个是否更好,因为在单击/突出显示每个文本视图后我需要执行不同的功能。如果是这样,如何实施?

【问题讨论】:

标签: java android xml android-layout


【解决方案1】:

您可以像这样以编程方式执行此操作:

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>

编辑:

对于您的具体情况如下:

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);

}

【讨论】:

  • 我明白了,但我问我是否按下它,然后它会保持不同的颜色,直到我再次按下它。
  • @XiJiaopin 嗯,这正是它的作用。只有在重新创建活动时才会重置。
  • 有什么办法可以做到一次只选择一个?
  • 是否必须取消选择第一个才能选择另一个?还是应该切换?
  • 假设选择了 TextView A。当 textview b 被触摸时,textview A 会自动被取消选择,而 textview b 是唯一被选中的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
相关资源
最近更新 更多