自定义带点击效果、计时功能、可圆角的Button(支持XML和代码修改属性)
来源:互联网 发布:notepad json 插件 编辑:程序博客网 时间:2024/05/18 00:11
由于实际开发中我们需要给很多按钮添加点击效果,按钮的点击效果添加的方法大致两种:
一、用selector写的XML,二、利用onTouch来写;本控件实现就是利用第二种。
继承Button,利用attrs给View添加自定义属性,onTouch给按钮添加点击效果,运用GradientDrawable 给View添加圆角,CountDownTimer 给View增加计时功能
package com.example.test;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.graphics.drawable.GradientDrawable;import android.os.CountDownTimer;import android.text.Spannable;import android.text.SpannableString;import android.text.TextUtils;import android.text.style.ForegroundColorSpan;import android.util.AttributeSet;import android.view.Gravity;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.TextView;/** * 重写Button,自定义Button样式 可在代码中或者XML文件中设置按钮的形状(是否圆角) 设置背景 设置文字颜色 * 设置点击效果(color支持String和Resources ID)等 * * @author Scw * @date 2015-12-1 */public class CustomButton extends Button { private boolean fillet = false;// 是否设置圆角 private float radius = 8;// 圆角半径 private int shape = 0;// 圆角样式,矩形、圆形等,由于矩形的Id为0,默认为矩形 private int backColori = 0;// 背景色,int类型 private int backColorSelectedi = 0;// 按下后的背景色,int类型 private int backGroundImage = 0;// 背景图,只提供了Id private int backGroundImageSeleted = 0;// 按下后的背景图,只提供了Id private int textColori = 0;// 文字颜色,int类型 private int textColorSeletedi = 0;// 按下后的文字颜色,int类型 private String backColors = "";// 背景色,String类型 private String textColors = "";// 文字颜色,String类型 private String textColorSeleteds = "";// 按下后的文字颜色,String类型 private String backColorSelecteds = "";// 按下后的背景色,String类型 private GradientDrawable gradientDrawable;// 控件的样式 private TimeLastUtil time; private boolean isStartTime = false; private boolean timeIsEnd = true; public CustomButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init(context, attrs); } public CustomButton(Context context, AttributeSet attrs) {this(context, attrs, 0);init(context, attrs); } public CustomButton(Context context) {this(context, null); } @SuppressLint({ "ClickableViewAccessibility", "Recycle" }) private void init(Context context, AttributeSet attrs) {time = new TimeLastUtil(10000, 1000, this, "S后重新发送");// 设置Touch事件setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent event) {// 按下改变样式setColor(event, arg0);if (isStartTime && timeIsEnd) { timeIsEnd = false; time.start();}return true; }});/* * 使用resalues/attrs.xml中的<declare- styleable>定义的Gallery属性. *//* 取得CustomButon属性的Index id */TypedArray typearray = context.obtainStyledAttributes(attrs, R.styleable.CustomButton);/* 让对象的styleable属性能够反复使用 */setBackColor(typearray.getColor(R.styleable.CustomButton_customBackground, 0));setBackColorSelected(typearray.getColor(R.styleable.CustomButton_clickeffect, 0));setFillet(typearray.getBoolean(R.styleable.CustomButton_fillet, fillet));setTextColori(typearray.getColor(R.styleable.CustomButton_customTextColor, Color.WHITE));setTextColorSelected(typearray.getColor(R.styleable.CustomButton_textclickeffect, 0));setRadius(typearray.getFloat(R.styleable.CustomButton_customRadius, radius));isStartTime=typearray.getBoolean(R.styleable.CustomButton_isStartTimer, isStartTime);// 将Button的默认背景色改为透明(透明色的资源ID无法获取)if (gradientDrawable == null) { gradientDrawable = new GradientDrawable();}if (0 == typearray.getInt(R.styleable.CustomButton_customBackground, 0)) { gradientDrawable.setColor(Color.TRANSPARENT);} else { setBackColor(typearray.getInt(R.styleable.CustomButton_customBackground, 0));}// 设置文字默认居中setGravity(Gravity.CENTER);typearray.recycle(); } public boolean setColor(MotionEvent event, View arg0) {if (MotionEvent.ACTION_DOWN == event.getAction()) { // 按下 if (0 != backColorSelectedi) {// 先判断是否设置了按下后的背景色int型if (fillet) { if (gradientDrawable == null) {gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(backColorSelectedi);} else { setBackgroundColor(backColorSelectedi);} } else if (!TextUtils.isEmpty(backColorSelecteds)) {if (fillet) { if (gradientDrawable == null) {gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.parseColor(backColorSelecteds));} else { setBackgroundColor(Color.parseColor(backColorSelecteds));} } // 判断是否设置了按下后文字的颜色 if (0 != textColorSeletedi) {setTextColor(textColorSeletedi); } else if (!textColorSeleteds.equals("")) {setTextColor(Color.parseColor(textColorSeleteds)); } // 判断是否设置了按下后的背景图 if (0 != backGroundImageSeleted) {setBackgroundResource(backGroundImageSeleted); }}if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_OUTSIDE|| event.getAction() == MotionEvent.ACTION_CANCEL) { // 抬起 if (0 == backColori && TextUtils.isEmpty(backColors)) {// 如果没有设置背景色,默认改为透明if (fillet) { if (gradientDrawable == null) {gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.TRANSPARENT);} else { setBackgroundColor(Color.TRANSPARENT);} } else if (0 != backColori) {if (fillet) { if (gradientDrawable == null) {gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(backColori);} else { setBackgroundColor(backColori);} } else {if (fillet) { if (gradientDrawable == null) {gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.parseColor(backColors));} else { setBackgroundColor(Color.parseColor(backColors));} } // 如果为设置字体颜色,默认为白色 if (0 == textColori && TextUtils.isEmpty(textColors)) {setTextColor(Color.WHITE); } else if (textColori != 0) {setTextColor(textColori); } else {setTextColor(Color.parseColor(textColors)); } if (backGroundImage != 0) {setBackgroundResource(backGroundImage); }}return arg0.onTouchEvent(event); } /** * 设置按钮的背景色,如果未设置则默认为透明 * * @param backColor */ public void setBackColor(String backColor) {this.backColors = backColor;if (backColor.equals("")) { if (fillet) {if (gradientDrawable == null) { gradientDrawable = new GradientDrawable();}gradientDrawable.setColor(Color.TRANSPARENT); } else {setBackgroundColor(Color.TRANSPARENT); }} else { if (fillet) {if (gradientDrawable == null) { gradientDrawable = new GradientDrawable();}gradientDrawable.setColor(Color.parseColor(backColor)); } else {setBackgroundColor(Color.parseColor(backColor)); }} } /** * 设置按钮的背景色,如果未设置则默认为透明 * * @param backColor */ public void setBackColor(int backColor) {this.backColori = backColor;if (backColori == 0) { if (fillet) {if (gradientDrawable == null) { gradientDrawable = new GradientDrawable();}gradientDrawable.setColor(Color.TRANSPARENT); } else {setBackgroundColor(Color.TRANSPARENT); }} else { if (fillet) {if (gradientDrawable == null) { gradientDrawable = new GradientDrawable();}gradientDrawable.setColor(backColor); } else {setBackgroundColor(backColor); }} } /** * 设置按钮按下后的颜色 * * @param backColorSelected */ public void setBackColorSelected(int backColorSelected) {this.backColorSelectedi = backColorSelected; } /** * 设置按钮按下后的颜色 * * @param backColorSelected */ public void setBackColorSelected(String backColorSelected) {this.backColorSelecteds = backColorSelected; } /** * 设置按钮的背景图 * * @param backGroundImage */ public void setBackGroundImage(int backGroundImage) {this.backGroundImage = backGroundImage;if (backGroundImage != 0) { setBackgroundResource(backGroundImage);} } /** * 设置按钮按下的背景图 * * @param backGroundImageSeleted */ public void setBackGroundImageSeleted(int backGroundImageSeleted) {this.backGroundImageSeleted = backGroundImageSeleted; } /** * 设置按钮圆角半径大小 * * @param radius */ public void setRadius(float radius) {if (gradientDrawable == null) { gradientDrawable = new GradientDrawable();}gradientDrawable.setCornerRadius(radius); } /** * 设置按钮文字颜色 * * @param textColor */ public void setTextColors(String textColor) {this.textColors = textColor;setTextColor(Color.parseColor(textColor)); } /** * 设置按钮文字颜色 * * @param textColor */ public void setTextColori(int textColor) {this.textColori = textColor;setTextColor(textColor); } /** * 设置按钮按下的文字颜色 * * @param textColor */ public void setTextColorSelected(String textColor) {this.textColorSeleteds = textColor; } /** * 设置按钮按下的文字颜色 * * @param textColor */ public void setTextColorSelected(int textColor) {this.textColorSeletedi = textColor; } /** * 按钮的形状 * * @param shape */ public void setShape(int shape) {this.shape = shape; } /** * 设置其是否为圆角 * * @param fillet */ @SuppressWarnings("deprecation") public void setFillet(Boolean fillet) {this.fillet = fillet;if (fillet) { if (gradientDrawable == null) {gradientDrawable = new GradientDrawable(); } // GradientDrawable.RECTANGLE gradientDrawable.setShape(shape); gradientDrawable.setCornerRadius(radius); setBackgroundDrawable(gradientDrawable);} } class TimeLastUtil extends CountDownTimer {/** * * @param millisInFuture * 总的时间 * @param countDownInterval * 计数间隔 * @param view * TextView或者Button * @param prompt * 自定义提示文字(默认"s后可重新发送") */public TimeLastUtil(long millisInFuture, long countDownInterval, TextView view, String prompt) { super(millisInFuture, countDownInterval);}@SuppressLint("NewApi")@Overridepublic void onFinish() { setText("获取验证码"); setClickable(true);// // 重新获得点击 setBackColor(Color.parseColor("#00CCCC"));// 还原背景色 timeIsEnd = true;}@Overridepublic void onTick(long millisUntilFinished) { setClickable(false);// 设置不能点击 setText(millisUntilFinished / 1000 + "s后可重新发送");// 设置倒计时时间 // 设置按钮为灰色,这时是不能点击的 setBackColor(Color.GRAY); Spannable span = new SpannableString(getText().toString());// 获取按钮的文字 span.setSpan(new ForegroundColorSpan(Color.RED), 0, String.valueOf(millisUntilFinished / 1000).length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);// 讲倒计时时间显示为红色 setText(span);} }}attrs
<declare-styleable name="CustomButton"> <!-- 按钮点击效果 (color) --> <attr name="clickeffect" format="color" /> <!-- 文字点击效果 (color) --> <attr name="textclickeffect" format="color" /> <!-- 按钮背景(图片,color) --> <attr name="customBackground" format="color" /> <!-- 文字颜色 (color) --> <attr name="customTextColor" format="color" /> <!-- 是否为圆角 true:是,false:否 --> <attr name="fillet" format="boolean" /> <!-- 圆角的角度(float) --> <attr name="customRadius" format="float" /> <!-- 是否开启计时器 true:是,false:否 --> <attr name="isStartTimer" format="boolean" /> </declare-styleable>
使用
<包名.CustomButton android:id="@+id/btn_share" android:layout_width="@dimen/dip_190" android:layout_height="@dimen/dip_35" android:layout_centerInParent="true" android:text="分享领红包" android:textColor="@android:color/white" android:visibility="invisible" app:clickeffect="@color/DE454A" app:customBackground="@color/primary" app:customRadius="100" app:fillet="true" app:isStartTimer="true"/>
xmlns:app="http://schemas.android.com/apk/res-auto"
《还有些许功能并不完善,后期会抽空写好上传》
源码下载地址http://download.csdn.net/detail/qq569699973/9506636
1 0
- 自定义带点击效果、计时功能、可圆角的Button(支持XML和代码修改属性)
- 简单的自定义Button 点击倒数计时
- 自定义Button点击效果
- Button自定义点击效果
- Android自定义Button的点击效果
- Android自定义Button的点击效果
- 通过代码实现button的点击效果
- 自定义控件集合属性的设计时支持
- Android自定义Button点击效果
- android自定义button点击效果
- Shape使用和button 的点击效果
- Button的监听事件和点击效果
- Android Button 带点击效果的圆角
- 自定义控件(四)自定义点击效果Button
- 自定义Button实现圆角和点击效果
- Android自定义点击带声音的控件TextView/Button/ListView
- v7修改Button颜色并保持默认的点击效果
- 自定义button的点击效果——颜色矩阵方法
- 利用python进行数据分析随笔小记
- Bluemix的三台虚拟机做MongoDB Replica Set
- GCD倒计时
- Qt QThread 这是我 见过解析最全面的一片文章
- 欧拉函数
- 自定义带点击效果、计时功能、可圆角的Button(支持XML和代码修改属性)
- Unreal Engine 4 —— 基于Kajiya-Kay的材质迭代
- 【菜鸟学安卓】- TextView 显示Html 一 解决图片显示不了的小问题
- 软件运行时的图标从文件中进行读取[C#]
- 模板类的继承
- RadioGroup中的RadioButton互斥
- 展开型博弈
- hiho #1014 : Trie树 (字典树的建立和查找)
- java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException