android自定义控件实现TextView按下后字体颜色改变

今天跟大家分享一下Android自定义控件入门,先介绍一个简单的效果TextView,按下改变字体颜色,后期慢慢扩展更强大的功能

直接看图片            

第一张是按下后截的图,功能很简单,也很容易实现,下面来看一下如何通过重写TextView来实现

一共三个文件  TextViewM.java,MainActivity.java,activity_textview.xml

TextViewM.java

  1 package landptf.control;
  2
  3 import android.content.Context;
  4 import android.graphics.Color;
  5 import android.util.AttributeSet;
  6 import android.view.MotionEvent;
  7 import android.view.View;
  8 import android.widget.TextView;
  9
 10 /**
 11  * 重写TextView 实现点击改变字体颜色
 12  * @author landptf
 13  * @date 2015-6-6
 14  */
 15 public class TextViewM extends TextView{
 16     private int textColori = 0;//控件的文字颜色,Int型
 17     private String textColors = "";//控件的文字颜色,String型
 18     private int textColorSeletedi = 0;//控件被按下后的文字颜色,Int型
 19     private String textColorSeleteds = "";//控件被按下后的文字颜色,String型
 20
 21     public TextViewM(Context context) {
 22         this(context, null);
 23     }
 24
 25     public TextViewM(Context context, AttributeSet attrs) {
 26         this(context, attrs, 0);
 27     }
 28
 29     /**
 30      * 实现TextView的构造方法
 31      * @param context
 32      * @param attrs
 33      * @param defStyle
 34      */
 35     public TextViewM(Context context, AttributeSet attrs, int defStyle) {
 36         super(context, attrs, defStyle);
 37         //设置TextView的Touch事件
 38         this.setOnTouchListener(new OnTouchListener() {
 39             @Override
 40             public boolean onTouch(View arg0, MotionEvent event) {
 41                 //设置颜色变化
 42                 setColor(event.getAction());
 43                 //注意此处的返回值,若想设置TextView的Click事件,则返回false
 44                 return true;
 45             }
 46         });
 47     }
 48     //设置颜色变化,该方法为private,不对外公开
 49     private void setColor(int state){
 50         try {
 51             //根据传过来的MotionEvent值来设置文字颜色
 52             if (state == MotionEvent.ACTION_DOWN) {
 53                 //鼠标按下
 54                 if (textColorSeletedi != 0) {
 55                     setTextColor(textColorSeletedi);
 56                 }else if (!textColorSeleteds.equals("")) {
 57                     setTextColor(Color.parseColor(textColorSeleteds));
 58                 }
 59             }
 60             if (state == MotionEvent.ACTION_UP) {
 61                 //鼠标抬起
 62                 if (textColori == 0 && textColors.equals("")) {
 63                     //如果为设置颜色值,则默认为黑色
 64                     setTextColor(Color.BLACK);
 65                 }else if (textColori != 0) {
 66                     setTextColor(textColori);
 67                 }else {
 68                     setTextColor(Color.parseColor(textColors));
 69                 }
 70             }
 71         } catch (Exception e) {
 72         }
 73
 74     }
 75
 76     /**
 77      * 设置文字的颜色
 78      * 为了不造成原setTextColor的冲突,在后面加“i”
 79      * @param color int类型
 80      */
 81     public void setTextColori(int color) {
 82         this.textColori = color;
 83         try {
 84             this.setTextColor(color);
 85         } catch (Exception e) {
 86         }
 87     }
 88
 89     /**
 90      * 设置文字的颜色
 91      * 为了不造成原setTextColor的冲突,在后面加“s”
 92      * @param color String类型
 93      */
 94     public void setTextColors(String color) {
 95         this.textColors = color;
 96         try {
 97             this.setTextColor(Color.parseColor(color));
 98         } catch (Exception e) {
 99         }
100     }
101
102     /**
103      * 设置文字被按下后的颜色
104      * @param color int类型
105      */
106     public void setTextColorSeleted(int textColorSeletedi) {
107         this.textColorSeletedi = textColorSeletedi;
108     }
109
110     /**
111      * 设置文字被按下后的颜色
112      * @param color String类型
113      */
114     public void setTextColorSeleted(String textColorSeleteds) {
115         this.textColorSeleteds = textColorSeleteds;
116     }
117 }

布局文件activity_textview.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     tools:ignore="HardcodedText" >
 7
 8     <landptf.control.TextViewM
 9         android:id="@+id/tvText1"
10         android:layout_width="match_parent"
11         android:layout_height="50dp"
12         android:background="#AA6666"
13         android:gravity="center"
14         android:text="TEXT1"
15         android:textSize="20sp" />
16
17     <landptf.control.TextViewM
18         android:id="@+id/tvText2"
19         android:layout_width="match_parent"
20         android:layout_height="50dp"
21         android:layout_below="@+id/tvText1"
22         android:layout_marginTop="50dp"
23         android:background="#66FF66"
24         android:gravity="center"
25         android:text="TEXT2"
26         android:textSize="20sp" />
27
28 </RelativeLayout>

测试类:MainActivity.java

 1 package landptf.control;
 2
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5
 6 /**
 7  * 测试类
 8  * @author landptf
 9  * @date 2015-6-6
10  */
11 public class MainActivity extends Activity {
12
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_textview);
17         initView();
18     }
19
20     //初始化控件
21     private void initView() {
22         TextViewM tvText1 = (TextViewM) findViewById(R.id.tvText1);
23         //对tvText1设置int型的颜色id
24         tvText1.setTextColori(android.graphics.Color.WHITE);
25         //按下的颜色
26         tvText1.setTextColorSeleted(android.graphics.Color.GRAY);
27         //对tvText2设置String型的颜色
28         TextViewM tvText2 = (TextViewM) findViewById(R.id.tvText2);
29         tvText2.setTextColors("#ffffffff");
30         //按下的颜色
31         tvText2.setTextColorSeleted("#ff888888");
32     }
33 }

代码实现的功能比较简单,可以在此基础上继续扩展,比如按下改变背景色等等。这样便可以省去好多xml文件,只通过封装几行代码就可以功能实现一些。

明天再写一个健壮一些的控件。

时间: 2024-08-09 06:24:23

android自定义控件实现TextView按下后字体颜色改变的相关文章

TextView 下划线/字体/颜色

TextView txtShuoming; SpannableStringBuilder builder =  new SpannableStringBuilder(getResources().getString(R.string.shuoming)); //1.设置字体颜色 builder.setSpan(new ForegroundColorSpan(Color.BLUE), 47,56, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //2.设置字体风格  

动态添加的RadioButoon实现字体颜色改变

我们都知道xml文件里写入的RadioButton可以给它的颜色设置一个selector,很轻松实现选中与未选中即点击后字体颜色发生改变,但是代码里动态加入的radioButton应该如何设置呢 今天为大家带来一个Demo有关动态添加的RadioButoon实现字体颜色改变 main_activity.xml:代码里写入两个固定的radioButton <?xml version="1.0" encoding="utf-8"?><LinearLay

echarts x轴或y轴文本字体颜色改变

1:x轴文本字体颜色改变 xAxis : [ { type : 'category', data : ['<30','30-','40-','50-','60-','>=70'], axisLabel: { show: true, textStyle: { color: '#fff' } } } ] 2:y轴文本字体颜色改变 yAxis : [ { type : 'value', name : '%', axisLabel : { formatter: '{value}', textStyle

Android自定义控件垂直TextView

正常情况下TextView的文本内容是水平显示的,那如何做到让内容垂直显示呢,于是做了一些尝试,自定义控件继承TextView,重写onDraw函数,代码如下: @Override protected void onDraw(Canvas canvas) { canvas.rotate(-90); canvas.translate(-getHeight(), 0); super.onDraw(canvas); } 以上实现确实做到了让内容垂直显示,但是存在宽度与高度无法适配的问题,比如在指定宽度

Android自定义控件(三)——打造闪闪发光的字体

介绍 在小米的开机动画和一些欢迎界面中, 我们经常看到这种闪闪发光的流光字体.看起来很炫酷,其实实现原理相当简单,我们只需要写自定义控件继承TextView,然后使用渲染器Gradient设置颜色渐变和Paint的setShadowLayer方法设置阴影,然后不断刷新改变位移即可. 实现 首先写一个shineTextView类继承自TextView. public class ShineTextView extends TextView { // 线性渐变渲染 private LinearGra

Android Them+SharedPreferences 修改程序所有view字体颜色、大小和页面背景

有这么一个需求,可以对页面的样式进行选择,然后根据选择改变程序所有字体颜色和页面背景.同时下一次启动程序,当前设置依然有效. 根据需求,我们需要一种快速,方便,有效的方式来实现需求,然后可以通过Android Them + SharedPreferences 来实现需求.Them用于存放设置的每一种样式,并应用于程序中,SharedPreferences用于记住程序当前的样式,根据SharedPreferences的内容来设置程序的样式,实现下次启动能够oncreat当前的样式设置. 这里的Th

设置TextView按下时变换文字颜色

在res中建立一个color文件夹,在其中新建一个xml(这里为text_color.xml): <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:color="@color/white"></item> <item androi

Android自定义控件:仿美团下拉菜单及相关代码优化

背景 最近的项目中用到了类似美团中的下拉多选菜单,在实际开发过程中,也发现了一些问题,主要归纳如下: 1.当菜单较为复杂时,如果不能设计好代码逻辑,将造成控件难于维护 2.美团菜单可以连续点击顶部tab,切换不同菜单,而我使用的popupWindow似乎在展开一个菜单时点击其他tab,菜单就会收回. 本文将针对如上两个问题进行一些讨论,最终给出较为合理的解决方案. 程序结构 由于菜单涉及多级多项,如果把UI和其他逻辑堆在一起写,必然会造成代码过于庞大,甚至没有办法扩展,更谈不上及时变更需求. V

Android开发之TextView的下划线添加

如何给TextView添加下划线呢,最近项目中需要这个,于是就用代码添加了下划线功能.主要就是用Paint的setFlags方法来实现,具体如下: ((TextView)mScrollView.findViewById(R.id.refresh)).getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); 就是获取TextView,然后获取Paint,再用Paint提供的setFlags方法设置下划线,该方法还可以设置其他的属性,比如颜色等