android - 想要 TextView 通过单击来改变颜色,就像单击按钮一样

标签 android android-layout

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

布局文件

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

相关文章:

android - 找不到 "libconscrypt_jni.so"

java - postDelayed方法中触发OnTouchlistener

android - 使用 Google Maps API v2 android 添加标记功能

Android:TextInputLayout 相互堆叠

java - 如何为布局的宽度和高度设置动画?

android - 在 Android View 上以编程方式设置 '?selectableItemBackground'

android - ScrollView 在我的相对布局中不起作用

java - 如何删除注入(inject)的 View ?

imageView 上的 android setAlpha 进入 CollapsingToolbarLayout 不起作用

java - 从 ViewHolder 中删除行