2010-12-17 17 views
21

ローカルリンクのように動作するテキストフィールドがあります。クリックすると、データベースからイメージがフェッチされて表示されます。常にサーバーにpingを実行しません。ここでandroid TextView:クリック時のテキストの色を変更する

ただ、質問私はテキストビューの色を見たいと思っているが、代わりに同じ黒い色の、黄色に変更する必要があります

テキストビュー

<TextView android:layout_marginLeft="2dp" android:linksClickable="true" 
      android:layout_marginRight="2dp" android:layout_width="wrap_content" 
      android:text="@string/Beatles" android:clickable="true" android:id="@+id/Beatles" 
      android:textColor="@color/Black" 
      android:textSize="12dp" android:layout_height="wrap_content" android:textColorHighlight="@color/yellow" android:textColorLink="@color/yellow" android:autoLink="all"></TextView> 

のためのXMLコードでありますボタンの振る舞いと同じですが、背景の色を変更する代わりに、私はテキストの色を変更したい

+0

ます。https:// stackoverflowの

この効果を達成するために、私は別のファイルに自分のonTouchListenerを実装しました.com/questions/5371719/change-clickable-textviews-color-on-focus-and-click – CoolMind

答えて

3

TextViewクラスを拡張する独自のTextViewクラスを作成し、onTouchEvent(MotionEvent event)

次に、渡されたMotionEventに基づいてインスタンスのテキストの色を変更できます。例えば

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     // Change color 
    } else if (event.getAction() == MotionEvent.ACTION_UP) { 
     // Change it back 
    } 
    return super.onTouchEvent(event); 
} 
23

を私はクリスティアンが示唆するもの好きですが、TextViewのを拡張することはやり過ぎのように思えます。さらに、彼の解決策はMotionEvent.ACTION_CANCELイベントを処理しないため、クリックが完了してもテキストが選択されたままになる可能性があります。

public class CustomTouchListener implements View.OnTouchListener {  
    public boolean onTouch(View view, MotionEvent motionEvent) { 
    switch(motionEvent.getAction()){    
      case MotionEvent.ACTION_DOWN: 
      ((TextView)view).setTextColor(0xFFFFFFFF); //white 
       break;   
      case MotionEvent.ACTION_CANCEL:    
      case MotionEvent.ACTION_UP: 
      ((TextView)view).setTextColor(0xFF000000); //black 
       break; 
    } 
     return false; 
    } 
} 

は、その後、あなたが望むものは何でものTextViewにこれを割り当てることができます:

newTextView.setOnTouchListener(new CustomTouchListener());

+1

あなたのコードは私のために働いてくれてありがとう。私はあなたの答えに真実を返し、私のために働いています.. –

関連する問題