以不同颜色突出显示所选文本视图

时间:2015-02-06 06:17:53

标签: android colors textview

我有3个文本视图,如下所示。一旦我点击其中一个,现在它变成红色并转回其默认颜色。我想将所选的textview保持为红色。我在片段中有这3个textview。

mQuickReturnView = (TextView) view.findViewById(R.id.footer);
        mQuickReturnView1 = (TextView) view.findViewById(R.id.footer1);
        mQuickReturnView2 = (TextView) view.findViewById(R.id.footer2);

        TextView clickTextView = (TextView) view.findViewById(R.id.footer);
        TextView clickTextView1 = (TextView) view.findViewById(R.id.footer1);
        TextView clickTextView2 = (TextView) view.findViewById(R.id.footer2);

        clickTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getActivity(), "toppings!",
                        Toast.LENGTH_LONG).show();
            }

        });
        clickTextView1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getActivity(), "Bigg pizza!",
                        Toast.LENGTH_LONG).show();
            }

        });
        clickTextView2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getActivity(), "Italiano!",
                        Toast.LENGTH_LONG).show();
            }

        });

.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- not selected has transparent color -->
    <item android:state_pressed="false" android:state_selected="false">
        <color android:color="#D8000000"/>
    </item>
    <item android:state_pressed="true" android:state_selected="false" >
        <color android:color="#ff0000"/>
    </item>
    <item android:state_pressed="false" android:state_selected="true">
        <color android:color="#ff0000"/>
    </item>
    <item android:state_pressed="true" android:state_selected="true">
        <color android:color="#ff0000"/>
    </item>
</selector>

布局

 <lk.gamma.pizzakraft.menu.QuickReturnListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/footer1"
        android:layout_width="106.5dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/textview_background"
        android:gravity="center"
        android:paddingBottom="8dip"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:textColor="@drawable/textview_font"
        android:paddingTop="8dip"
        android:text="@string/footer3"
        android:textAppearance="?android:attr/textAppearanceSmall"
        />
    <!-- android:background="#D8000000" -->

    <TextView
        android:id="@+id/footer2"
        android:layout_width="107dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/footer"
        android:background="@drawable/textview_background"
        android:gravity="center"
        android:paddingBottom="8dip"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:paddingTop="8dip"
        android:textColor="@drawable/textview_font"
        android:text="@string/footer2"
        android:textAppearance="?android:attr/textAppearanceSmall"
         />

    <TextView
        android:id="@+id/footer"
        android:layout_width="106dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/textview_background"
        android:gravity="center"
        android:paddingBottom="8dip"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:paddingTop="8dip"
        android:textColor="@drawable/textview_font"
        android:text="@string/footer1"
        android:textAppearance="?android:attr/textAppearanceSmall"
         />

enter image description here

3 个答案:

答案 0 :(得分:3)

有一个简单的解决方案:

private static TextView selectedView = null;

... ...

eachTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            //change last selected view to be normal
            if(selectedView != null)
                 selectedView.setTextColor(Color.parseColor("#D8000000"));

            //set selectedView to be current selected view
            selectedView = (TextView)v;

            //change selected view to be red
            selectedView.setTextColor(Color.parseColor("#FF0000"));



            Toast.makeText(getActivity(), "toppings!",
                    Toast.LENGTH_LONG).show();
        }

    });

如果要更改TextView背景颜色insdead的文本颜色,那么只需替换&#39; setTextColor&#39;使用&#39; setBackgroundColor&#39;。

除了你的情况,似乎没有必要使用选择器,因为它只是用于特定的视图状态更改,与其他视图无关。

答案 1 :(得分:0)

你可以以编程方式完成。 例如,将此代码放在clickTextView.setOnClickListener()中。

clickTextView.setTextColor(Color.parseColor("#FF0000"));

文字视图将变为红色。

为此,您不需要包含颜色规则的xml文件。

答案 2 :(得分:0)

尝试使用以下代码:

clickTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
              changeViewBackground(true,false,false);
        }

    });
    clickTextView1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            changeViewBackground(false,true,false);
        }

    });
    clickTextView2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            changeViewBackground(false,false,true);
        }

    });


    private void changeViewBackground(boolean view1,boolean view2,boolean  
        view3) {    
            clickTextView.setSelected(view1);
            clickTextView1.setSelected(view2);
            clickTextView2.setSelected(view3);
      }