0

私はtextViewでLinearLayoutを持っています。セレクタからtextViewの色を変更する方法

は私がレイアウトを押したとき、それは

私の問題は、レイアウト

を押したときにも、テキストの色を変更する方法である背景色を変更

android:background="@drawable/tab_bg_selector" 

ので、セレクタからのLinearLayoutの背景を設定しています

私はonStateChangeListnerのようなものが必要なので、ユーザーがレイアウトを押すとテキストの色も変わります

ありがとう、

Tomer

+0

セレクタをtextViewにも使用したい場合はnaを入力しますか? – Praveenkumar

+0

はい、状態がセレクタで変更されたときにテキストの色を変更できるようにしたい場合は –

答えて

1

ただ、このコードを試す -

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 
public void clickme(View view) 
{ 
    TextView tv = (TextView)findViewById(R.id.textView1); 
    tv.setTextColor(Color.RED);    
} 

Main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:onClick="clickme" 
android:orientation="vertical" > 


<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
</LinearLayout> 

あなたのセレクタでこれを試してみてください。

+0

これを試しても、関数は決して呼び出されません –

+0

何ですか?呼び出されませんか?ちょうどそれをデバッグする? – Praveenkumar

+0

コードを完全にコピーしましたか? – Praveenkumar

3

レイアウトのonClickに

textview.setTextColor(色)これを置きます。

+0

私はすべての状態のセレクタを持っていますので、助けにはならない、私はちょうどtextViewの色を変更する必要がある –

1
は、XMLタグ用いて二つの方法 でのTextView色を設定することができ

:Javaコードの「方法で 又は 設定色 "のTextView object.setTextColor(は、colorName)" を "アンドロイドtextcolorは=" colorvalueを

4

How to make shape's child TextView white when state_pressed="true"の重複はかなり正解であるduplicateParentState =「真」のTextViewの子供に親ListViewコントロールの選択状態を転送し、私のために働い以下:。。

<TextView 
    android:duplicateParentState="true" 
    android:textColor="@drawable/text_selector" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

とtext_selector:

あなたは

android:background="@drawable/tab_bg_selector"

その間違いなく作品のような上で適用するように、その中

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
     android:color="@color/text_on" /> <!-- pressed --> 
    <item android:state_focused="true" 
     android:color="@color/text_on" /> <!-- focused --> 
    <item android:color="@color/text_off" /> <!-- default --> 
</selector> 
1

が一つの解決策がある 第1のセレクタを設定されている 2つ目のテキストの色と異なる色の背景にある2つのイメージを作成します

関連する問題