繁体   English   中英

更改所选文字的颜色

[英]Change textview color on selected

我需要的是,单击/轻按时,TextView的颜色变为红色,但是当我不再点击它时,它必须回到原始的白色。

这是我的.xml:

    <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/vu"
android:id="@+id/textView"
android:gravity="center"
android:textSize="60sp"
android:textColor="#ffffff"
android:background="#212121"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

这是我的.java:

public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    TextView tv = (TextView) findViewById(R.id.textView);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Light.ttf");
    tv.setTypeface(typeface);
    WebView webView = (WebView) findViewById(R.id.webView);
    webView.loadUrl("http://www.papery.hol.es");
    webView.setWebViewClient(new WebViewClient());
    webView.setVerticalScrollBarEnabled(false);
    webView.setOverScrollMode(View.OVER_SCROLL_NEVER);
    // Look up the AdView as a resource and load a request.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
    }
}

/ res / color /文件夹中创建颜色状态列表资源 ,如下所示:

/res/color/text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#ff0000" />
    <item android:color="@android:color/white"/>
</selector>

然后使用此资源作为文本颜色:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    android:textColor="@color/text_color"
    android:clickable="true"
    />

请注意,要使此方法起作用,您必须将android:clickable属性设置为true或在代码中的textview上设置单击侦听器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM