触摸更改 TextView 背景颜色

Changing TextView Background color on touch

我想在点击或触摸文本视图时更改它的背景颜色。 这是文本视图

<com.example.shuvo.KeyboardButton
                android:id="@+id/showMenuButton"
                style="@drawable/color"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:alpha="0.8"
                android:text="Menu"
                android:textColor="@drawable/text_link_selector"
                android:background="@drawable/keyboard"
                 />

在keyboard.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">  <color android:color="#00ff00" /> </item>

并在 keyboardButton.java

public class KeyboardButton extends TextView {

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

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

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

public void setLayoutParams(int width, int height) {
    LayoutParams params = new LayoutParams(width, 60);
    this.setLayoutParams(params);
    setBackgroundResource(R.drawable.keyboard);
    setPadding(10, 10, 10, 10);
}

}

这不起作用。问题出在哪里?

最好的方法是使用 onTouchListener 或 onClickListener:

    final com.example.shuvo.KeyboardButton text = findViewById(R.id.showMenuButton);
    text.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // change the background color
            text.setBackgroundColor(color);
            text.setBackgroundResource(drawable);
            text.setTextColor(color);
            return false;
        }
    });

在可绘制文件夹中创建 .xml 文件并将此代码放入其中

<item android:drawable="@color/red" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@color/red" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@color/red" android:state_focused="true"/>
<item android:drawable="@color/green" android:state_focused="false" android:state_pressed="false"/>

在此之后将此 xml 设置为文本视图中的背景

<TextView android:id="@+id/txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:background="@drawable/yourxmlfile" />

其实我刚刚解决了这个问题。需要在自定义textview中添加android:clickable="true"。现在一切正常。但是我保留这个问题是为了帮助别人。

最好的方法是使用选择器xml

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

<item android:state_focused="true" android:drawable="@color/red"/>
<item android:drawable="@color/white"/>

</selector>

在Activity中我们也可以像

那样改变它
view.setOnTouchListener(new View.OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
      switch(event.getAction())
      {
          case MotionEvent.ACTION_DOWN:
              v.setBackgroundResource(R.drawable.pressed);
              lastY = event.getY();
              break;
          case MotionEvent.ACTION_UP:
              v.setBackgroundResource(R.drawable.normal);
              break;
          case MotionEvent.ACTION_MOVE:
              float abs = Math.abs(lastY - event.getY());

              if(abs > TIME_DELAY) // TIME_DELAY=2
                  v.setBackgroundResource(R.drawable.normal);
              break;
      }
      return true;
  }
});

您可以将时间延迟更改得尽可能低,以便背景快速变化。