让TextView中变为橙色聚焦时? [英] Make TextView turn orange when focused?

查看:139
本文介绍了让TextView中变为橙色聚焦时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextView,我将其设置为可点击和可聚焦 - 我怎么得到它突出橙色(如按钮),当用户使用拨轮等重点是:

I have a textview, I set it as clickable and focusable - how do I get it to highlight to orange (like a button) when the user focuses it with the trackwheel etc?:

TextView tv = ...;
tv.setClickable(true);
tv.setFocusable(true);

感谢

推荐答案

这是很容易的。这里是溶液

This is quite easy. Here is the solution.

您有背景的TextView元素属性设置为 @绘制/ tbselector 是这样的。

You have an TextView element with its background attribute set to @drawable/tbselector like this.

<TextView android:text="My text" 
    android:id="@+id/tv01"
    android:layout_width="300dip"
    android:layout_height="150dip"
    android:layout_gravity="center_horizontal"  
    android:background="@drawable/tbselector"/>

最后一个属性的android:背景是必不可少的其他的东西是你的

The last attribute android:background is essential the other stuff is up to you.

现在你在你的绘制子目录中创建一个 tbselector.xml 。它看起来像这样。

Now you create a tbselector.xml in your drawable subdirectory. Which looks like this.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/bgdefault"
        android:state_focused="false"
        android:state_selected="false"/>
    <item
        android:drawable="@drawable/bgselected"
        android:state_focused="true"
        android:state_selected="false"/>
</selector>

现在你创建一个 bgdefault.xml 绘制子目录,它看起来像这样。

Now you create a bgdefault.xml in your drawable subdirectory which looks like this.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="200dip"
        android:height="150dip"
        android:color="#00FF00"/>
    <solid
        android:color="#00FF00"/>
</shape>

最后,创建一个 bgselected.xml 绘制子目录,它看起来像另外一个与像其他颜色值例如,这

Finally create a bgselected.xml in your drawable subdirectory which looks like the other one with other color values like this for example.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="200dip"
        android:height="150dip"
        android:color="#FFFF00"/>
    <solid
        android:color="#FFFF00"/>
</shape>

和多数民众赞成你现在有一个国家依赖TextView的背景。但是,您可以决定设置你的可绘制在你选择XML这完全取决于你。我的价值观是你示区别只是随机的值。

And thats it you now have a state dependent TextView background. You can however decide to set your drawables in your selector XML it's totally up to you. My values are just random values to show you the difference.

希望它帮助。

这篇关于让TextView中变为橙色聚焦时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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