在触摸时更改TextView背景颜色

3

当点击或触摸文本视图时,我希望更改其背景颜色。 这是一个文本视图。

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

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

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


onTouch() 的代码在哪里? - Murtaza Khursheed Hussain
不需要使用Java动态解决此问题,只需在TextView中添加android:clickable="true"即可。 - Niloy Shuvo
4个回答

4
在drawable文件夹中创建.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的背景

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

1
要在单击时更改背景颜色,需要添加 android:clickable="true" - Niloy Shuvo
是的,但默认文本视图是可点击的,所以如果您的文本视图没有响应,则需要将此行放在文本视图内部。 - Ravi Makvana

3

最好的方法是使用一个选择器 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;
  }
});

您可以将时间延迟尽量降低,以便背景更快地变化。


1
实际上,我刚刚解决了这个问题。需要在自定义文本视图中添加android:clickable="true"。现在它可以正常工作了。但我将保留这个问题以帮助其他人。

0
最好的方法是使用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;
        }
    });

它只会在按下时更改背景,然后不会改回来。您必须检查 if(event.getAction() == MotionEvent.ACTION_DOWN)ACTION_UP。但这很有问题,因为如果您按住并向左或向右滑动按钮,则背景颜色将保持不变,除非您再次点击以重置它。 - AlwaysConfused
@StuckBetweenTrees 是的,你说得对,但我认为 OP 只是在询问如何在单击按钮时仅更改一次背景,而不是具有按下释放效果。 - Muhammed Refaat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接