突出在不同颜色的选择的TextView [英] highlight the selected textview in different colour
问题描述
我有3个文本的意见,如下图所示。当我点击它的一个,现在它变成红色,把背上它的默认颜色。我想保持选定的TextView的红色。我有一个片段,这3个TextView的。
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文件
<?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"
/>
推荐答案
有一个简单的解决方案:
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();
}
});
如果你想改变文字颜色TextView的背景颜色insdead,那么只需更换setTextColor与setBackgroundColor。
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屋!