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

直接看图片            

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

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

TextViewM.java

 package landptf.control;

 import android.content.Context;
 import android.graphics.Color;
 import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.view.View;
 import android.widget.TextView;

 /**
  * 重写TextView 实现点击改变字体颜色
  * @author landptf
  * @date 2015-6-6
  */
 public class TextViewM extends TextView{
     private int textColori = 0;//控件的文字颜色,Int型
     private String textColors = "";//控件的文字颜色,String型
     private int textColorSeletedi = 0;//控件被按下后的文字颜色,Int型
     private String textColorSeleteds = "";//控件被按下后的文字颜色,String型

     public TextViewM(Context context) {
         this(context, null);
     }

     public TextViewM(Context context, AttributeSet attrs) {
         this(context, attrs, 0);
     }

     /**
      * 实现TextView的构造方法
      * @param context
      * @param attrs
      * @param defStyle
      */
     public TextViewM(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
         //设置TextView的Touch事件
         this.setOnTouchListener(new OnTouchListener() {
             @Override
             public boolean onTouch(View arg0, MotionEvent event) {
                 //设置颜色变化
                 setColor(event.getAction());
                 //注意此处的返回值,若想设置TextView的Click事件,则返回false
                 return true;
             }
         });
     }
     //设置颜色变化,该方法为private,不对外公开
     private void setColor(int state){
         try {
             //根据传过来的MotionEvent值来设置文字颜色
             if (state == MotionEvent.ACTION_DOWN) {
                 //鼠标按下
                 if (textColorSeletedi != 0) {
                     setTextColor(textColorSeletedi);
                 }else if (!textColorSeleteds.equals("")) {
                     setTextColor(Color.parseColor(textColorSeleteds));
                 }
             }
             if (state == MotionEvent.ACTION_UP) {
                 //鼠标抬起
                 if (textColori == 0 && textColors.equals("")) {
                     //如果为设置颜色值,则默认为黑色
                     setTextColor(Color.BLACK);
                 }else if (textColori != 0) {
                     setTextColor(textColori);
                 }else {
                     setTextColor(Color.parseColor(textColors));
                 }
             }
         } catch (Exception e) {
         }

     }

     /**
      * 设置文字的颜色
      * 为了不造成原setTextColor的冲突,在后面加“i”
      * @param color int类型
      */
     public void setTextColori(int color) {
         this.textColori = color;
         try {
             this.setTextColor(color);
         } catch (Exception e) {
         }
     }

     /**
      * 设置文字的颜色
      * 为了不造成原setTextColor的冲突,在后面加“s”
      * @param color String类型
      */
     public void setTextColors(String color) {
         this.textColors = color;
         try {
             this.setTextColor(Color.parseColor(color));
         } catch (Exception e) {
         }
     }

     /**
      * 设置文字被按下后的颜色
      * @param color int类型
      */
     public void setTextColorSeleted(int textColorSeletedi) {
         this.textColorSeletedi = textColorSeletedi;
     }

     /**
      * 设置文字被按下后的颜色
      * @param color String类型
      */
     public void setTextColorSeleted(String textColorSeleteds) {
         this.textColorSeleteds = textColorSeleteds;
     }
 }

布局文件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

 package landptf.control;

 import android.app.Activity;
 import android.os.Bundle;

 /**
  * 测试类
  * @author landptf
  * @date 2015-6-6
  */
 public class MainActivity extends Activity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_textview);
         initView();
     }

     //初始化控件
     private void initView() {
         TextViewM tvText1 = (TextViewM) findViewById(R.id.tvText1);
         //对tvText1设置int型的颜色id
         tvText1.setTextColori(android.graphics.Color.WHITE);
         //按下的颜色
         tvText1.setTextColorSeleted(android.graphics.Color.GRAY);
         //对tvText2设置String型的颜色
         TextViewM tvText2 = (TextViewM) findViewById(R.id.tvText2);
         tvText2.setTextColors("#ffffffff");
         //按下的颜色
         tvText2.setTextColorSeleted("#ff888888");
     }
 }

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

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

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

  1. [转载]Linux下终端字体颜色设置方法

    原文地址:Linux下终端字体颜色设置方法作者:router 网上类似的文章有很多,但是都是转来转去的,没有经过测试,按照很多文章的方法会造成你设置之后的终端在换行和删除输入字符时终端显示会乱七八糟, ...

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

    1:x轴文本字体颜色改变 xAxis : [ { type : 'category', data : ['<30','30-','40-','50-','60-','>=70'], axi ...

  3. $Android自定义控件在不同状态下的属性

    在写代码的时候,有时候需要控件在不同状态下显示不同的外观,比如在按钮按下的时候要变颜色,EditText获取焦点时候边框要变颜色等.那么下面就来梳理一下这些是怎么实现的. (一)按钮按下时候变颜色 1 ...

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

    有这么一个需求,可以对页面的样式进行选择,然后根据选择改变程序所有字体颜色和页面背景.同时下一次启动程序,当前设置依然有效. 根据需求,我们需要一种快速,方便,有效的方式来实现需求,然后可以通过And ...

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

    在res中建立一个color文件夹,在其中新建一个xml(这里为text_color.xml): <selector xmlns:android="http://schemas.and ...

  6. QSS QPushButton:hover :pressed ...为状态下变更字体颜色(color)无效,变成字体粗细(font-weight)有效???

    //字体颜色变更无效 QPushButton:hover{ font-weight:bold; color:rgba(, , , ); } //字体颜色变更有效 QPushButton#pushBut ...

  7. Android自定义控件之仿美团下拉刷新

    美团的下拉刷新分为三个状态: 第一个状态为下拉刷新状态(pull to refresh),在这个状态下是一个绿色的椭圆随着下拉的距离动态改变其大小. 第二个部分为放开刷新状态(release to r ...

  8. Android自定义控件之TextView

    转自:http://labs.easymobi.cn/?p=284 有时候Android自带的控件无法满足我们的某些要求,这时就需要我们自定义控件来实现这些功能.比如需要一个TextView里的字倾斜 ...

  9. Android开发之TextView的下划线添加

    如何给TextView添加下划线呢,最近项目中需要这个,于是就用代码添加了下划线功能.主要就是用Paint的setFlags方法来实现,具体如下: ((TextView)mScrollView.fin ...

随机推荐

  1. 查看oracle SID

    源地址:http://blog.sina.com.cn/s/blog_5f20c4740100dodl.html SID就是数据库的实例 select instance_name from  V$in ...

  2. 20145305《JAVA程序设计》课程总结

    每周读书笔记链接汇总 问卷调查 第0周学习心得 第1周学习总结 第2周学习总结 第3周学习总结 第4周学习总结 第5周学习总结 第6周学习总结 第7周学习总结 第8周学习总结 第9周学习总结 第10周 ...

  3. apache下php无法解析直接显示源代码解

    在http.conf中加入php的设置 #php5_start phpIniDir "d:/Program Files/php" LoadModule php5_module &q ...

  4. 理论沉淀:RANSAC算法

    1.解决问题: 当一组样本数据中含有(较小波动的)正常数据(inliers)和(较大波动的)异常数据(outliers)且异常数据的量还不小于正常数据的量时,用最小二乘法将难以获得期望的直线(即能拟合 ...

  5. (easy)LeetCode 263.Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  6. [HDU 4417] Super Mario (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给你n个数,下标为0到n-1,m个查询,问查询区间[l,r]之间小于等于x的数有多少个 ...

  7. python 实例属性之单,双下划线

    具体区别看下面例子 class A: def __init__(self,name='Andy'): self._name = name class B: def __init__(self,name ...

  8. Ubuntu上部署Ghost博客

    所有文章搬运自我的个人主页:sheilasun.me 刚刚成功把自己的ghost博客部署到Linode VPS上了,在这里回顾并顺便整理一下从购买域名到部署代码到服务器的整个过程. 购买域名 万网或者 ...

  9. Flex4 DataGrid实现可复制单元格,同时解决自定义GridItemRenderer出现1009错误的方法

    原创内容,如需转载,请注明出处,谢谢 最近在项目中发现Flex的DataGrid不支持内容复制,在涉及到保护敏感数据时倒是很有用处,但大部分情况下,我们还是希望客户能够直接复制DataGrid单元格中 ...

  10. Windows 7的100M隐藏分区

    1.Windows 7的100MB的隐藏分区是Windows 7的活动分区,类似于Linux的/boot. 这其实有点类似Linux的做法,Linux在安装过程中可以专门分出一个100MB左右的分区作 ...