以不同的颜色突出显示所选的textview [英] highlight the selected textview in different colour

查看:193
本文介绍了以不同的颜色突出显示所选的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.0encoding =utf-8?> 
< selector xmlns:android =http://schemas.android.com/apk/res/android>

<! - 未选择透明色 - >
< item android:state_pressed =falseandroid:state_selected =false>
< color android:color =#D8000000/>
< / item>
< item android:state_pressed =trueandroid:state_selected =false>
< color android:color =#ff0000/>
< / item>
< item android:state_pressed =falseandroid:state_selected =true>
< color android:color =#ff0000/>
< / item>
< item android:state_pressed =trueandroid: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
: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
/

解决方案

有一个简单的解决方案:

  private static TextView selectedView = null; 

... ...

eachTextView.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick (View v){

//将上次选择的视图更改为正常
if(selectedView!= null)
selectedView.setTextColor(Color.parseColor(#D8000000) );

//将selectedView设置为当前所选视图
selectedView =(TextView)v;

//将所选视图更改为红色
selectedView.setTextColor(Color.parseColor(#FF0000));



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

});

如果你想改变TextView的背景颜色insdead文本颜色,那么只需将'setTextColor' setBackgroundColor'。



除了你的情况,似乎不是必须使用选择器,因为它只是为特定的视图状态变化,与其他视图无关。 / p>

I have 3 text views as shown below. once i click on one of it, now it turns to red and turn backs to its default colour. I want to keep the selected textview in red. i have these 3 textview in a fragment.

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 file

<?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>

layout

 <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"
         />

解决方案

There is one simple solution:

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

    });

If you want to change TextView background color insdead of text color, then just replace 'setTextColor' with 'setBackgroundColor'.

Besides for your case, seems it is not neccessary to use selector, because it is just for specific view state change, nothing to do with other view.

这篇关于以不同的颜色突出显示所选的textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆