【问题标题】:Changing TextView background color on click android单击android时更改TextView背景颜色
【发布时间】:2013-09-27 00:17:33
【问题描述】:

我正在尝试在单击时更改文本视图的背景。

例如,如果单击文本视图,则背景会变为黄色并保持黄色,直到再次单击为止。然后它返回到默认背景。

目前textview在按下时背景会发生变化,但在释放时返回默认值。

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

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

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

活动

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

【问题讨论】:

  • 你想改变点击文本视图的背景吗?

标签: android textview android-background


【解决方案1】:

如果有人需要它(Kotlin)。这可以在点击时切换 TextView 的背景和文本颜色。

ToggleTextView.kt

class ToggleTextView : AppCompatTextView {
    private var mfilterSelected = false
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int): super(context, attrs, defStyle) {}`

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}

    constructor(context: Context, checkableId: Int) : super(context) {}

    constructor(context: Context) : super(context) {}

    fun isFilterSelected(): Boolean {
        return mfilterSelected
    }

    fun toggleFilterState() {
        if (mfilterSelected) {
            background = resources.getDrawable(R.drawable.toggle_1)
            setTextColor(resources.getColor(R.color.gray))
            mfilterSelected = false
        } else {
            background = resources.getDrawable(R.drawable.toggle_2)
            setTextColor(resources.getColor(R.color.white))
            mfilterSelected = true
        }
    }
}

--toggle_1.xml--
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<stroke android:width="1dp" android:color="@color/gray" />
<corners android:radius="10dp" />
<solid android:color="@color/white" />
</shape>
--toggle_2.xml--
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners android:radius="10dp" />
<solid android:color="@color/orange" />
</shape>
Click listener in your Activity/Fragment:
yourTextView?.setOnClickListener {
            yourTextView.toggleFilterState()
        }
Usage in our xml layout file:
<ToggleTextView
                android:id="@+id/yourTextView"
                android:background="@drawable/toggle_1"
                android:clickable="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:lines="1"/>

【讨论】:

    【解决方案2】:

    如果单击文本视图,则背景变为黄色并保持黄色,直到再次单击。然后它返回到默认背景。

    这是一个逻辑问题,因为您需要在点击侦听器中保持当前的点击状态。(盲编码):

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

    为了改进答案,您应该在活动中保留stateChanged 标志并在活动重新创建时保留其值 - 如果用户轮换活动。 (标志存储在onSaveInstanceState,如果参数不为空,则恢复到onCreate。)

    【讨论】:

    • 不客气......只要确保在活动中保留该标志
    【解决方案3】:

    除了上面的答案,也试试这个代码sn-p。

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

    希望对大家有用。

    【讨论】:

    【解决方案4】:

    onCreate() 方法中,

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

    并将监听器设置为textview:

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

    最后点击:

    @Override
    public void onClick(View v) {
    
    ll.setBackgroundColor(whatever color);
    or If you want to change the text background color,
    
    tv1.setBackground(whatevercolor);
    
    }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案5】:

      为您的 Textview

      使用 onclicklistner()

      在你的听众中使用

            txt.setBackgroundColor(Color.RED); 
      

      例如

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

      【讨论】:

      • 有什么理由不使用布尔值作为 0 / 1?
      • 可能是,他来自C背景:P
      猜你喜欢
      • 2014-06-12
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      • 2019-09-01
      • 2013-11-28
      相关资源
      最近更新 更多