gpt4 book ai didi

android - 单击android更改TextView背景颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:07 25 4
gpt4 key购买 nike

我试图在单击时更改 TextView 的背景。

例如,如果单击 TextView ,背景将变为黄色并保持黄色直到再次单击。然后它返回到其默认背景。

目前 textview 的背景在按下时改变,但在释放时返回到默认值。

我已经在互联网上搜索解决方案并查看了 stackoverflow 上的大部分解决方案,仍然没有解决方案。

可绘制/选择器.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>

可绘制/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>

可绘制/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>

文本样式:

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

谁能告诉我我做错了什么

谢谢。

我的解决方案:

    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>

Activity

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());
}

最佳答案

除了上面的答案,也试试这个代码 fragment 。

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

希望对大家有用。

关于android - 单击android更改TextView背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18936597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com