2013-09-21 67 views
6

Tôi đang cố gắng thay đổi nền của chế độ xem văn bản khi được nhấp.Thay đổi màu nền của TextView khi nhấp vào android

Ví dụ: nếu chế độ xem văn bản nhấp vào thì nền sẽ chuyển sang màu vàng và vẫn màu vàng cho đến khi nó được nhấp lại. Sau đó, nó trở về nền mặc định của nó.

Hiện tại, chế độ xem văn bản thay đổi nền khi được nhấn xuống, nhưng trở về mặc định khi phát hành.

Tôi đã tìm kiếm trên Internet các giải pháp và xem xét hầu hết tất cả các giải pháp trên stackoverflow, vẫn không có giải pháp.

drawable/selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/circle_on" android:state_enabled="true" android:state_pressed="true"/> 
    <item android:drawable="@drawable/circle_on" android:state_enabled="true" android:state_focused="true"/> 
    <item android:drawable="@drawable/circle_off"/> 
</selector> 

drawable/circle_on:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" > 
    <stroke 
    android:width="2dp" 
    android:color="@color/Gray" > 
    </stroke> 
    <solid android:color="@color/LightBlue" /> 
</shape> 

drawable/circle_off:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" > 
    <stroke 
     android:width="2dp" 
     android:color="@color/Gray" > 
    </stroke> 
    <solid android:color="@color/WhiteSmoke" /> 
</shape> 

TextView:

<TextView 
       style="@style/RoundText" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:background="@drawable/repeat_selector" 
       android:clickable="true" 
       android:text="Sun" > 
      </TextView> 

Text Style:

<style name="RoundText"> 
    <item name="android:textColor">#555555</item> 
    <item name="android:gravity">center_vertical|center_horizontal</item> 
    <item name="android:textSize">15sp</item> 
    <item name="android:textStyle">bold</item> 
    <item name="android:fontFamily">sans-serif-thin</item> 
</style> 

Có thể ai đó xin vui lòng cho tôi biết những gì tôi đang làm sai

Cảm ơn.

Giải pháp MY:

public class PlanTextView extends TextView { 

private boolean _stateChanged; 
private boolean _selected; 

public boolean is_stateChanged() { 
    return _stateChanged; 
} 

public void set_stateChanged(boolean _stateChanged) { 
    this._stateChanged = _stateChanged; 
} 

public boolean is_selected() { 
    return _selected; 
} 

public void set_selected(boolean _selected) { 
    this._selected = _selected; 
} 

public PlanTextView(Context context) { 
    super(context); 
} 

public PlanTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public PlanTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 
} 


<com.plan.views.PlanTextView 
       android:id="@+id/mon" 
       style="@style/RoundText" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:background="@drawable/circle_off" 
       android:clickable="true" 
       android:onClick="PlanOnClick" 
       android:text="mon" > 
</com.plan.views.PlanTextView> 

Hoạt động

public void PlanOnClick(View v) { 
    PlanTextView view = (PlanTextView)v; 
    if (view.is_stateChanged()) { 
     view.setBackgroundResource(R.drawable.circle_off); 
     view.set_selected(false); 
    } else { 
     view.setBackgroundResource(R.drawable.circle_on); 
     view.set_selected(true); 
    } 
    view.set_stateChanged(!view.is_stateChanged()); 
} 
+0

Bạn có muốn thay đổi nền khi nhấp vào một lần xem văn bản không? – mike20132013

Trả lời

6

Nếu TextView được nhấp những thay đổi nền sang màu vàng và vẫn còn màu vàng cho đến khi nó nhấp một lần nữa. Sau đó, nó trở về nền mặc định của nó.

Đó là một vấn đề của logic như bạn cần phải giữ trong người nghe nhấp chuột tình trạng nhấp chuột hiện tại (mù mã hóa):.

textView.setOnClickClickListener(new View.OnClickListener() { 
    private boolean stateChanged; 
    public void onClick(View view) { 
     if(stateChanged) { 
      // reset background to default; 
      textView.setBackgroundDrawable(circleOffDrawable); 
     } else { 
      textView.setBackgroundDrawable(circleOnDrawable); 
     } 
     stateChanged = !stateChanged; 
    } 
}); 

Để cải thiện câu trả lời, bạn nên giữ stateChanged cờ trong hoạt động và giữ lại giá trị của nó trên các hoạt động tái tạo - nếu người dùng xoay hoạt động. (Lưu trữ cờ trong onSaveInstanceState và khôi phục trong onCreate nếu tham số của nó không phải là rỗng.)

+0

cảm ơn. nó làm việc – capiono

+0

bạn được chào đón ... chỉ cần đảm bảo bạn giữ lại cờ đó trong các hoạt động tái tạo – gunar

1

sử dụng một onclicklistner () cho bạn TextView

Trong listener của bạn sử dụng

 txt.setBackgroundColor(Color.RED); 

Ví dụ

if(count==1) 
    { 
     txt.setBackgroundColor(Color.YELLOW); 
     count=0; 
     } 
     else 
    if(count==0) 
     { 
     txt.setBackgroundColor(Color.RED); 
     count==1; 
      } 
+0

Bất kỳ lý do nào bạn không sử dụng boolean cho 0/1? – JakeSteam

+0

Có thể, anh ấy đến từ ** C ** background: P –

0

Trong phương pháp onCreate(),

LinearLayout(or Whatever layout you are using) ll = (LinearLayout)findViewById(R.id.mainlay); 

và thiết lập người nghe trên textview:

TextView tv1 = (TextView)findViewById(R.id.maintext); 

tv1.setOnClickListener(this); 

Và cuối cùng trên nhấp chuột:

@Override 
public void onClick(View v) { 

ll.setBackgroundColor(whatever color); 
or If you want to change the text background color, 

tv1.setBackground(whatevercolor); 

} 

Hope this helps.

5

Ngoài các câu trả lời ở trên, hãy thử đoạn mã này quá.

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true"> 
     <shape> 
     <gradient android:endColor="#AD1F2D" android:startColor="#AD1F2D" /> 
     </shape> 
    </item> 
    <item android:state_focused="true"> 
     <shape> 
     <gradient android:endColor="#ff4b46" android:startColor="#ff4b46" /> 
     </shape> 
    </item> 
    <item> 
     <shape> 
     <gradient android:endColor="#ff4b46" android:startColor="#ff4b46" /> 
     </shape> 
    </item> 

</selector> 

Tôi hy vọng điều này sẽ hữu ích cho mọi người.

Các vấn đề liên quan