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#中利用LightningChart绘制曲线图表
最近在做一个“基于C#语言的电炉温控制软件设计”的设计,我在大学并不是专业学习C#语言编程的,对C#的学习研究完全是处于兴趣,所以编程技术也不是很厉害,遇到问题多参照网络上的开源码. 这不,在做这个课 ...
- Java多线程--JDK并发包(1)
Java多线程--JDK并发包(1) 之前介绍了synchronized关键字,它决定了额一个线程是否可以进入临界区:还有Object类的wait()和notify()方法,起到线程等待和唤醒作用.s ...
- mahout 使用
最近在做mahout源码调用的时候,发现一个参数:startPhase和endPhase,这两个参数是什么意思呢?比如运行RecommenderJob时,可以看到10个MR任务,所以猜测是否是一个ph ...
- C#转换成Json的方法集
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Te ...
- 华中农业大学第五届程序设计大赛网络同步赛-D
Problem D: GCD Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 179 Solved: 25[Submit][Status][Web B ...
- ActiveReports 报表控件V12新特性 -- 文本框和标签控件的浓缩
ActiveReports是一款专注于 .NET 平台的报表控件,全面满足 HTML5 / WinForms / ASP.NET / ASP.NET MVC / WPF 等平台下报表设计和开发工作需求 ...
- 单表(SSM、SpringBoot、SpringCloud、Freemaker、BootStrap等)
山门也有门门道道, 开发.测试.安卓...... 小子被纳入MIS小山峰,虽不及BOP势力庞大,高手如云, 仅寥寥七人, 却也于入小山峰之事乐趣至极. 前几日峰主布下一道新手任务, 制作一张单表并运行 ...
- [WPF 知识总结系列] —— 基本控件的简单样式集合
一.ScrollBar <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta ...
- 如何避免SHRINKDATABASE & SHRINKFILE 产生索引碎片(转载)
1. TRUNCATEONLY参数的使用我们在建立的Job中通常使用如下的语法DBCC SHRINKDATABASE (N'DB', 10,TruncateOnly)其中TruncateOnly的用处 ...
- mysql client之init-command
If the server is a replication master and you want to avoid replicating the content to replication s ...