Android设置常见控件点击效果
一. Imageview的点击效果——图片稍微变暗突出点击效果
public class ClickImageView extends AppCompatImageView {
public ClickImageView(Context context) {
super(context);
}
public ClickImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ClickImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.setColorFilter(0x99000000);
return true;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
this.setColorFilter(null);
break;
}
return super.onTouchEvent(event);
}
}
二. Button、TextView的点击效果
- 仅仅突出点击效果(点击之后不需要特定颜色)——用
style
(不用shape
、selector
),当需要波纹效果可设置foreground
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="点击效果"
android:textColor="#DDFFFFFF"
android:foreground="?android:attr/selectableItemBackground"//波纹效果
android:background="@drawable/shape_5dp_blue"
style="@style/Widget.AppCompat.Button"/>
//设置弧度、颜色(shape_5dp_blue)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<solid android:color="@android:color/holo_blue_dark"/>
</shape>

- 设置点击和非点击两种状态特定颜色——用
shape
、selector
,当需要波纹效果可设置foreground
,当也需要底部点击阴影效果可设置style
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="点击效果"
android:textColor="#DDFFFFFF"
android:foreground="?android:attr/selectableItemBackground"//当需要波纹效果
android:background="@drawable/selector_5dp_blue"

style="@style/Widget.AppCompat.Button"
/>
(selector_5dp_blue)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="@android:color/holo_red_dark"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape>
<solid android:color="@android:color/holo_blue_dark"/>
<corners android:radius="5dp"/>
</shape>
</item>
</selector>

- 当有的要求更高,需要设置波纹效果的颜色——用
ripple
(只能在21或以上使用,所以新建drawable-v21,都是命名selector_5dp_blue
)
(selector_5dp_blue)
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@android:color/holo_red_dark">
<item>
<selector>
<item android:state_pressed="true">
<shape>
<solid android:color="@android:color/holo_red_dark"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape>
<solid android:color="@android:color/holo_blue_dark"/>
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
</item>
</ripple>

作者:我想成为创业者
链接:https://www.jianshu.com/p/68fac9baf077
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
Android设置常见控件点击效果的更多相关文章
- Android 中常见控件的介绍和使用
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
- 【ALearning】第三章 Android基本常见控件
本章主要介绍主要的寻常较多使用的控件,包含TextView.EditView.ImageView.Button等.本章将介绍相关控件基本属性的使用,为以后章节的进阶学习提供基础.案例中引用的Linea ...
- Android -- 常见控件的小效果
1,EditText控件 ① 修改光标颜色 自定义drawable 创建cursor.xml文件 <?xml version="1.0" encoding="utf ...
- Android ListView 子控件点击事件
android:descendantFocusability beforeDescendants:viewgroup会优先其子类控件而获取到焦点 afterDescendants:viewgroup只 ...
- Android的GridView控件点击图片变暗效果
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setC ...
- Android自己定义控件系列五:自己定义绚丽水波纹效果
尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...
- 【Android】10.0 UI开发——如何编写程序界面、常见控件的使用
************************ 转载请注明出处:https://www.cnblogs.com/xiaofu007/p/10331880.html ***************** ...
- 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式
注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式 这个坑,必须要注意呀, 比如在用ListView的时候,如果在List_ ...
- android中RecyclerView控件实现点击事件
RecyclerView控件实现点击事件跟ListView控件不同,并没有提供类似setOnItemClickListener()这样的注册监听器方法,而是需要自己给子项具体的注册点击事件. 本文的例 ...
随机推荐
- 第一个微信小程序
微信官方已经开放微信小程序的官方文档和开发者工具.前两天都是在看相关的新闻来了解小程序该如何开发,这两天官方的文档出来之后,赶紧翻看了几眼,重点了解了一下文档中框架与组件这两个部分,然后根据简易教程, ...
- c#里面如何激活一个外部程序进程并显示在最前
using System.Diagnostics; using System.Runtime.InteropServices; [DllImport("user32.dll")] ...
- Java虚拟机--虚拟机类加载机制
虚拟机类加载机制 虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. 类的生命周期如下: 加载 ...
- oracle数据库无法导出空表的问题解决(开始于oracle11g)
--设置系统参数 alter system set deferred_segment_creation=false; Select 'alter table '||table_name||' allo ...
- MySQL数据库的配置
一.配置MySQL数据库 MySQL的官网www.mysql.com 1.解压绿色版mysql,并改名为mysql5.7,如下图 对比一下下图5.6以前的版本,少data目录(存放数据)和my-def ...
- 使用Druid网上监控
0.添加依赖 <!--druid连接池--> <dependency> <groupId>com.alibaba</groupId> <artif ...
- VS C#文件的复制
/// <summary> /// 复制目录 /// </summary> /// <param name="OldDirectoryPath"> ...
- npm包管理工具在一般项目中的应用方法
最近自己在有时间,在通学一些知识点,记录一下,以便以后使用方面 当我们在做项目的时候,如果需要到包管理工具,那么我们一定会经历以下流程: 1.首先在官网下载node.js,然后默认安装到C盘 检查是否 ...
- PHP实现批量删除(封装)
前台 <!DOCTYPE html> <html> <head> <title>批量删除</title> </head> < ...
- Python爬虫入门教程石家庄链家租房数据抓取
1. 写在前面 这篇博客爬取了链家网的租房信息,爬取到的数据在后面的博客中可以作为一些数据分析的素材.我们需要爬取的网址为:https://sjz.lianjia.com/zufang/ 2. 分析网 ...