我想让用户在单击文本字段时感觉就像我们单击按钮一样,释放按钮后,它会在很短的时间内变为橙色,闪烁一次,然后再次变为第一种颜色。当我点击它时,它运行良好,但没有出现颜色闪烁。
布局文件
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_below="@+id/button5"
android:layout_marginLeft="35dp"
android:layout_marginTop="28dp"
android:clickable="true"
android:onClick="onClick"
android:text="Click Me"
android:textAppearance="?android:attr/textAppearanceLarge" />
主 Activity 代码
textV.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent("com.DRMS.help"));
}
});
最佳答案
您可以使用OnTouchListener
,也可以使用选择器。
textV.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// change color
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
// set to normal color
}
return true;
}
});
编辑:
您也可以使用选择器。边框和圆角矩形。自定义相同的。
可绘制文件夹中的bkg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/pressed" />
<item android:state_focused="false"
android:drawable="@drawable/normal" />
</selector>
drawable文件夹中的normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#0AECBF"/>
<stroke android:width="3dp"
android:color="#0FECFF" />
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
drawable文件夹中的pressed.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff33ffff" />
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
现在设置背景
android:background="@drawable/bkg"
关于android - 想要 TextView 通过单击来改变颜色,就像单击按钮一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17945176/