Ändern TextView Hintergrundfarbe auf auf android

Ich versuche, den hintergrund ändern, der eine textview, wenn geklickt wird.

Zum Beispiel, wenn die textview ist auf es der hintergrund wechselt zu gelb und bleibt gelb, bis es wieder auf. Dann kehrt er zurück zu seiner Standard-hintergrund.

Derzeit textview ändert sich der hintergrund auf gedrückt, aber wenn Sie zurück zu default-release".

Habe ich die Suche im internet nach Lösungen und schauen sich die meisten die Lösung auf stackoverflow immer noch keine Lösung.

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 Stil:

  <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>

Kann mir bitte jemand sagen was ich falsch mache

Dank.

MEINE Lösung:

    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>

Aktivität

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());
}
Möchten Sie den hintergrund ändern, klicken Sie auf ein textview?

InformationsquelleAutor capiono | 2013-09-21

Schreibe einen Kommentar