问题描述
我有一个文本字段,它的行为类似于本地链接,单击它会从数据库中获取图像并显示它.它不会一直 ping 到服务器.
I have a textfield that behaves like a local link, clicking on it fetches an image from database and shows it. It doesn't ping to server all the time.
这里是文本视图的xml代码
Here is the xml code for the text view
<TextView android:layout_marginLeft="2dp" android:linksClickable="true"
android:layout_marginRight="2dp" android:layout_width="wrap_content"
android:text="@string/Beatles" android:clickable="true" android:id="@+id/Beatles"
android:textColor="@color/Black"
android:textSize="12dp" android:layout_height="wrap_content" android:textColorHighlight="@color/yellow" android:textColorLink="@color/yellow" android:autoLink="all"></TextView>
问题是我想看到文本视图的颜色应该改为黄色,而不是相同的黑色,
The question is i want to see the color of text view should be changed in yellow, instead of the same black color,
就像按钮行为一样,但我想更改文本颜色而不是更改背景颜色
Just Like the button behavior but instead of changing the background color i want to change a text color
推荐答案
我喜欢 Cristian 的建议,但扩展 TextView 似乎有点矫枉过正.此外,他的解决方案不处理 MotionEvent.ACTION_CANCEL
事件,因此即使点击完成后,您的文本也可能保持选中状态.
I like what Cristian suggests, but extending TextView seems like overkill. In addition, his solution doesn't handle the MotionEvent.ACTION_CANCEL
event, making it likely that your text will stay selected even after the clicking is done.
为了达到这个效果,我在一个单独的文件中实现了自己的onTouchListener:
To achieve this effect, I implemented my own onTouchListener in a separate file:
public class CustomTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
((TextView)view).setTextColor(0xFFFFFFFF); //white
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
((TextView)view).setTextColor(0xFF000000); //black
break;
}
return false;
}
}
然后您可以将其分配给您希望的任何 TextView:
Then you can assign this to whatever TextView you wish:
newTextView.setOnTouchListener(new CustomTouchListener());
这篇关于android TextView:单击时更改文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!