用TextView实现button点击效果
来源:互联网 发布:开源的数据库管理工具 编辑:程序博客网 时间:2024/05/18 13:31
TextView是Android开发中最常用的控件,它的Api也十分丰富。这里仅仅介绍下使用TextView来实现的Button,点击效果的实现。
UI页面中任何一个可点击的控件,都应该有点击效果。
当TextView所在的控件被点击时,可以从以下三处实现点击效果:
1.如果TextView的drawableLeft、….drawableBottom属性赋有值,可以将icon的图标通过布局文件标签的方式实现;
如:res/drawable/xxx_selector.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/xxx_pressed" /> <item android:drawable="@drawable/xxx_normal" android:state_pressed="true"/></selector>
2.给TextView的父节点设置background,其background通过布局文件实现,如:res/drawable/common_cancel_btn_selector.xml
<?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="#f0f0f0" /> </shape> </item> <item> <shape> <solid android:color="#ffffff" /> </shape> </item></selector>
3.设置TextView的textColor,textColor通过布局文件标签实现,如:res/color/common_black_text_color_selector.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#323232" android:state_pressed="true" /> <item android:color="#323232" android:state_selected="true" /> <item android:color="#808080" /></selector>
例如如下效果:
使用布局文件的是
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f9f9f9" android:gravity="center" android:orientation="horizontal"> <LinearLayout android:id="@+id/ll_cancel" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="1" android:background="@drawable/common_cancel_btn_selector" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/tv_cancel" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="取消" android:textColor="@color/common_black_text_color_selector" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:id="@+id/ll_sure" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="1" android:background="@drawable/common_sure_btn_selector" android:orientation="horizontal"> <TextView android:id="@+id/tv_sure" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:text="确定" android:textColor="@color/common_white_text_color_selector" android:textSize="16sp" /> </LinearLayout></LinearLayout>
同时需要在代码中设置ll_sure、ll_cancel的点击侦听:
ll_cancel.setOnClickListener(this); ll_sure.setOnClickListener(this);
如果需要在代码中动态修改,实现方式如下:
tv_sure.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0);ll_sure.setBackgroundResource(R.drawable.common_sure_btn_selector);tv_sure.setTextColor(getResources().getColorStateList(R.color.common_white_text_color_selector));
0 0
- 用TextView实现button点击效果
- [Android] ImageButton | Button | TextView 点击和触摸效果实现
- textview添加selector实现button的点击效果
- textview中点击效果实现,比如点击textview中实现图片和文字的颜色变化(类似于button)
- Android实现Button点击效果
- android button 点击效果实现
- Android之实现TextView控件圆角以及Button点击、焦点效果
- 通过代码实现button的点击效果
- Android linearlayout实现button的点击效果
- Android TextView实现点击展开动画效果
- 实现TextView中link的点击效果
- android TextView 实现自定义文字点击效果
- TextView实现超链接并自定义点击效果
- Android问题集(二)——TextView在点击时显示不同颜色,Button点击效果
- 自定义Button点击效果
- Button自定义点击效果
- button点击效果
- button点击效果
- 剑-表示数值的字符
- neo4j入门教程-java中使用Cypher操作数据库
- springMVC 的modelandView
- node.js中express-session配置项详解
- Mavlink地面站编写之二--Mission Planner编译
- 用TextView实现button点击效果
- 实现简单的输入编辑,录入正确的字符串,出现‘$’,表示撤销前一个输入,发现连续的‘$’,表示连续撤销前面的输入;发现‘%’,表示放弃前面所有录入;生成最终结果。
- margin和padding的区别
- android开发游记:meterial design 5.0 新增控件介绍及使用方法
- 从jvm编译优化角度解读String的赋值比较
- 让你的web应用更安全
- 无源码动态调试APK
- HelloCSDN
- 解释器模式