2015-02-05 427 views
0

我有3個文本視圖,如下所示。一旦我點擊其中一個,它就會變成紅色,但當我取消選擇它時,會回到它的默認顏色。我想將選定的TextView保持爲紅色。我有這3個TextViews在一個片段。選中時突出顯示TextView,並在選中後保持突出顯示

mQuickReturnView = (TextView) view.findViewById(R.id.footer); 
mQuickReturnView1 = (TextView) view.findViewById(R.id.footer1); 
mQuickReturnView2 = (TextView) view.findViewById(R.id.footer2); 

TextView clickTextView = (TextView) view.findViewById(R.id.footer); 

clickTextView.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Toast.makeText(getActivity(), "I just clicked my textview!",Toast.LENGTH_LONG).show(); 
    } 
}); 

xml。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- not selected has transparent color --> 
    <item android:state_pressed="false" android:state_selected="false"> 
     <color android:color="#D8000000"/> 
    </item> 
    <item android:state_pressed="true" > 
     <color android:color="#ff0000"/> 
    </item> 
    <item android:state_pressed="false" android:state_selected="true"> 
     <color android:color="#ff0000"/> 
    </item> 
</selector> 

我應該更改一次以便在選擇後將它保持爲紅色。

+0

添加onClickListener,然後實施顏色更改。 – Jimi 2015-02-05 14:27:28

+0

[如何點擊或點擊TextView文本]的可能重複(http://stackoverflow.com/questions/3328757/how-to-click-or-tap-on-a-textview-text) – Tascalator 2015-02-05 14:30:41

+0

@John David你解決了這個問題嗎? – Marcus 2015-02-07 11:39:54

回答

1

您可以使用switch case聲明來實現此目的。

首先添加的onClick監聽到你的TextView小號

mQuickReturnView = (TextView) view.findViewById(R.id.footer); 
mQuickReturnView1 = (TextView) view.findViewById(R.id.footer1); 
mQuickReturnView2 = (TextView) view.findViewById(R.id.footer2); 

mQuickReturnView.setOnClickListner(this); 
mQuickReturnView1.setOnClickListner(this); 
mQuickReturnView2.setOnClickListner(this); 

然後執行類似下面的onClick方法。

@Override 
public void onClick(View v) { 
    switch(v.getId()){ 
     /*First TextView was clicked, set it as your clicked color and 
      the others as your default, non-clicked color. */ 
     case R.id.footer: 
      mQuickReturnView.setBackgroundColor(Color.GREEN); 
      mQuickReturnView1.setBackgroundColor(Color.BLACK); 
      mQuickReturnView2.setBackgroundColor(Color.BLACK)); 
     break; 

     /*Second TextView was clicked, set it as your clicked color and 
      the others as your default, non-clicked color. */ 
     case R.id.footer1: 
      mQuickReturnView.setBackgroundColor(Color.BLACK); 
      mQuickReturnView1.setBackgroundColor(Color.GREEN); 
      mQuickReturnView2.setBackgroundColor(Color.BLACK); 
     break; 

     /*Third TextView was clicked, set it as your clicked color and 
      the others as your default, non-clicked color. */ 
     case R.id.footer2: 
      mQuickReturnView.setBackgroundColor(Color.BLACK); 
      mQuickReturnView1.setBackgroundColor(Color.BLACK); 
      mQuickReturnView2.setBackgroundColor(Color.GREEN); 
     break; 
    } 
} 
+0

Marcus,我想改變textview背景顏色,而不是文本顏色 – 2015-02-06 04:34:43

+0

如果你想改變背景顏色,你只需調用'mQuickReturnView.setBackgroundColor(Color.GREEN);'而不是'mQuickReturnView.setColor(Color.GREEN );'。我已經更新了我的答案。 @JohnDavid – Marcus 2015-02-07 11:40:45