我需要的是,当点击/点击时,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);
}
}
答案 0 :(得分:1)
在 / res / color / 文件夹中创建color state list resource,如下所示:
<强> /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上设置点击监听器。