gpt4 book ai didi

android - 希望 textview 像单击按钮一样通过单击更改颜色

转载 作者:搜寻专家 更新时间:2023-11-01 08:57:27 29 4
gpt4 key购买 nike

我想让用户在点击一个文本字段时感觉就像我们点击一​​个按钮,在释放按钮后它会在很短的时间内变成橙色,然后闪烁并再次变成它的第一种颜色。当我点击它时它运行良好但没有出现颜色闪烁。

布局文件

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com