更改TextView触摸的背景颜色
我想在点击或触摸时更改textview的背景颜色。 这是TextView的更改TextView触摸的背景颜色
<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);
}
}
这不行。哪里有问题?
其实我刚才解决了这个问题。需要在自定义文本视图中添加android:clickable="true"
。现在这工作正常。但我保留这个问题来帮助别人。
做到这一点,最好的方法是使用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;
}
});
它只会改变按下bg,然后不会变回。你必须检查if(event.getAction()== MotionEvent.ACTION_DOWN)'和'ACTION_UP'。但是,这是错误的,因为如果你按住并在按钮上向左或向右滑动,bg颜色将保持不变并且不会变回,除非再次点击它来重置它 – AlwaysConfused 2017-04-22 00:26:52
@StuckBetweenTrees是的,你说得对,但我相信OP在点击按钮时只询问一次背景变化,而没有按下释放效果。 – 2017-04-22 08:59:50
创建绘制文件夹.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" />
要点击更改bg颜色,需要添加'android:clickable =“true”' – 2015-04-06 07:50:23
yes但默认文本视图是可点击的,所以如果您的文本视图没有响应,则不需要添加,然后将此行放入textview中。 – 2015-04-06 07:55:02
<?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>
在活动我们也可以改变它像
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;
}
});
您可以更改时间延迟尽可能低,这样的背景变化快
的最佳方式。
哪里是'onTouch()'的代码? – 2015-04-06 07:37:58
不需要用java动态地解决这个问题,通过在textview中添加'android:clickable =“true”'会起作用。 – 2015-04-06 07:47:59