我刚刚设置过一个按钮点击后的颜色,想在按钮的MotionEvent.ACTION_UP:时,恢复刚刚设置的颜色
就是点击下去时,变一个颜色,离开按钮时,恢复原来的颜色哈。
buttonTubo.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// buttonTubo.setBackgroundColor(getResources().getColor(R.color.blue));
buttonTubo.setTextColor(getResources().getColor(R.color.tuyu_setting_opration));
break;
case MotionEvent.ACTION_UP:
// buttonTubo.setBackgroundColor(getResources().getColor(R.color.white));
// buttonTubo.setTextColor(null);
Intent intent = new Intent(getActivity(), AcTuboHomePager.class);
startActivity(intent);
break;
}
return true;
}
});
在其中 用了 buttonTubo.setTextColor(null); 就会闪退。
你不是已经写了吗?没有效果吗
break写错位置了,提前跳出switch了
想要实现这个效果,可以使用selector ,没必要自己实现
如下,按下黑色,移走手指就是白色
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_pressed="false"/>
<item android:color="@color/black" android:state_pressed="true"/>
</selector>
/**
* Sets the text color.
*
* @see #setTextColor(int)
* @see #getTextColors()
* @see #setHintTextColor(ColorStateList)
* @see #setLinkTextColor(ColorStateList)
*
* @attr ref android.R.styleable#TextView_textColor
*/
@android.view.RemotableViewMethod
不允许设置为空,请看源码
public void setTextColor(ColorStateList colors) {
if (colors == null) {
throw new NullPointerException();
}
mTextColor = colors;
updateTextColors();
}