簡體   English   中英

當狀態在Android中的TextView中更改時更改textColor

[英]Changing the textColor when the state changes in TextView in Android

嘿,我試圖在用戶按下TextView時更改textColor 我正在嘗試使類似Windows 8超鏈接按鈕。 我在res/color folder有此選擇器。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_hovered="true">
        <color android:color="@color/darkBlue"/>
    </item>
    <item  android:state_pressed="true">
        <color android:color="@color/lightBlue"/>
    </item>
    <item android:color="@color/black"/> <!-- default color -->
</selector>

我這樣用

<TextView
        android:id="@+id/tw_language"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/language_label"
        android:layout_marginRight="3dp"
        android:clickable="true"
        android:autoLink="all"
        android:text="@string/default_language_label"
        android:textColor="@color/language_button"

        />

在活動中,當我獲得對此mLanguage.setPaintFlags(mLanguage.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);的引用時,我設置了mLanguage.setPaintFlags(mLanguage.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

獲得帶下划線的文本。 使用此應用程序將崩潰。 如果我設置

android:background="@color/language_button"

代替textColor它可以正常工作。 有人知道我在做什么錯嗎?

您需要將屬性添加到TextView中,如下所示

<TextView
    android:id="@+id/txtResult"
    style="@drawable/language_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
/>

style="@drawable/language_button"是您的選擇器文件。 我已經在drawable/stack.xml目錄中定義了該文件。

你可以這樣做:

textView = (TextView)findViewById(R.id.myTextView);
    mMainView.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            textView.setTextColor(Color.GREEN);//set the color here
        }

    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM