Android 自定义数字加减器
该自定义View主要是实现一款效果不错的数字加减器的功能的,但是也可以自定义选择器的外观颜色等。
1、自定义View的布局(add_sub_view.xml)
<?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="@android:color/white"
android:orientation="horizontal"> <Button
android:id="@+id/bt01"
android:layout_width="32dp"
android:layout_height="32dp"
android:scaleType="fitCenter"
android:textSize="18sp"/> <TextView
android:id="@+id/et01"
android:layout_width="32dp"
android:layout_height="32dp"
android:enabled="false"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#000000"
android:textSize="16sp">
</TextView> <Button
android:id="@+id/bt02"
android:layout_width="32dp"
android:layout_height="32dp"
android:scaleType="fitCenter"
android:textSize="18sp"/> </LinearLayout>
2、定义该View的一些属性(attrs.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AddAndSubView">
<attr name="textColor" format="color"/>
<attr name="initValue" format="integer"/>
<attr name="maxValue" format="integer"/>
<attr name="minValue" format="integer"/>
<attr name="textSize" format="dimension"/>
<attr name="textFrameBackground" format="reference|color"/>
<attr name="addBackground" format="reference|color"/>
<attr name="subBackground" format="reference|color"/>
<attr name="textFrameWidth" format="dimension"/>
<attr name="addWidth" format="dimension"/>
<attr name="subWidth" format="dimension"/>
<attr name="addText" format="string"/>
<attr name="subText" format="string"/>
</declare-styleable> </resources>
3、定义该View的Java类(AddAndSubView.java)
package com.ileevey.addsub; import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView; public class AddAndSubView extends LinearLayout{ /** 显示文本 */
private TextView mTextView;
/** 增加按钮 */
private Button btAdd;
/** 减少按钮 */
private Button btReduce;
/** 显示文本的长宽 */
private int textFrameWidth;
/** 显示文本及button中文字的颜色 */
private int textColor;
/** 初始值 */
private int initValue;
/** 最大值 */
private int maxValue;
/** 最小值 */
private int minValue;
/** 显示文本及button中文字的大小 */
private int textSize;
/** 显示文本的背景 */
private Drawable textFrameBackground;
/** 增加按钮的背景 */
private Drawable addBackground;
/** 减少按钮的背景 */
private Drawable subBackground;
/** 增加按钮的大小 */
private int addWidth;
/** 减少按钮的大小 */
private int subWidth;
/** 增加按钮中的文本 */
private String addText;
/** 减少按钮中的文本 */
private String subText; public AddAndSubView(Context context, AttributeSet attrs) {
super(context, attrs);
initWidget(context);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AddAndSubView);
textColor = a.getColor(R.styleable.AddAndSubView_textColor, getResources().getColor(android.R.color.black));
textSize = a.getDimensionPixelSize(R.styleable.AddAndSubView_textSize, 16);
textFrameBackground = a.getDrawable(R.styleable.AddAndSubView_textFrameBackground);
textFrameWidth = a.getDimensionPixelSize(R.styleable.AddAndSubView_textFrameWidth, 48);
addBackground = a.getDrawable(R.styleable.AddAndSubView_addBackground);
subBackground = a.getDrawable(R.styleable.AddAndSubView_subBackground);
initValue = a.getInt(R.styleable.AddAndSubView_initValue, 0);
maxValue = a.getInt(R.styleable.AddAndSubView_maxValue, 1000000000);
minValue = a.getInt(R.styleable.AddAndSubView_minValue, -1000000000);
addWidth = a.getDimensionPixelSize(R.styleable.AddAndSubView_addWidth, 48);
subWidth = a.getDimensionPixelSize(R.styleable.AddAndSubView_subWidth, 48);
addText = a.getString(R.styleable.AddAndSubView_addText);
subText = a.getString(R.styleable.AddAndSubView_subText);
setAddBackground(addBackground);
setAddText(addText);
setAddWidth(addWidth);
setInitValue(initValue);
setMaxValue(maxValue);
setMinValue(minValue);
setSubBackground(subBackground);
setSubText(subText);
setSubWidth(subWidth);
setTextColor(textColor);
setTextFrameBackground(textFrameBackground);
setTextFrameWidth(textFrameWidth);
setTextSize(textSize);
a.recycle();
} protected void onFinishInflate() {
super.onFinishInflate();
addListener(); } public void initWidget(Context context){
LayoutInflater.from(context).inflate(R.layout.add_sub_view, this);
mTextView = (TextView)findViewById(R.id.et01);
btAdd = (Button)findViewById(R.id.bt01);
btReduce = (Button)findViewById(R.id.bt02);
} public void addListener(){
btAdd.setOnClickListener(new OnClickListener() { public void onClick(View v) { int num = Integer.valueOf(mTextView.getText().toString());
num++;
if (num >= maxValue+1)
return;
mTextView.setText(Integer.toString(num));
}
}); btReduce.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
int num = Integer.valueOf(mTextView.getText().toString());
num--;
if (num <= minValue-1)
return;
mTextView.setText(Integer.toString(num));
}
});
} public int getTextFrameWidth() {
return textFrameWidth;
} public void setTextFrameWidth(int textFrameWidth) {
this.textFrameWidth = textFrameWidth;
mTextView.setWidth(textFrameWidth);
mTextView.setHeight(textFrameWidth);
} public int getTextColor() {
return textColor;
} public void setTextColor(int textColor) {
this.textColor = textColor;
mTextView.setTextColor(textColor);
btAdd.setTextColor(textColor);
btReduce.setTextColor(textColor);
} public int getInitValue() {
return initValue;
} public void setInitValue(int initValue) {
this.initValue = initValue;
mTextView.setText(String.valueOf(initValue));
} public int getMaxValue() {
return maxValue;
} public void setMaxValue(int maxValue) {
this.maxValue = maxValue;
} public int getMinValue() {
return minValue;
} public void setMinValue(int minValue) {
this.minValue = minValue;
} public int getTextSize() {
return textSize;
} public void setTextSize(int textSize) {
this.textSize = textSize;
mTextView.setTextSize(textSize);
} public Drawable getTextFrameBackground() {
return textFrameBackground;
} public void setTextFrameBackground(Drawable textFrameBackground) {
this.textFrameBackground = textFrameBackground;
mTextView.setBackgroundDrawable(textFrameBackground);
} public Drawable getAddBackground() {
return addBackground;
} public void setAddBackground(Drawable addBackground) {
this.addBackground = addBackground;
Resources res = getResources();
int color = res.getColor(android.R.color.darker_gray);
Drawable drawable = new ColorDrawable(color);
btAdd.setBackgroundDrawable(addBackground==null?drawable:addBackground);
} public Drawable getSubBackground() {
return subBackground;
} public void setSubBackground(Drawable subBackground) {
this.subBackground = subBackground;
Resources res = getResources();
int color = res.getColor(android.R.color.darker_gray);
Drawable drawable = new ColorDrawable(color);
btReduce.setBackgroundDrawable(subBackground==null?drawable:subBackground);
} public int getAddWidth() {
return addWidth;
} public void setAddWidth(int addWidth) {
this.addWidth = addWidth;
btAdd.setWidth(addWidth);
btAdd.setHeight(addWidth);
} public int getSubWidth() {
return subWidth;
} public void setSubWidth(int subWidth) {
this.subWidth = subWidth;
btReduce.setWidth(subWidth);
btReduce.setHeight(subWidth);
} public String getAddText() {
return addText;
} public void setAddText(String addText) {
this.addText = addText;
btAdd.setText(addText);
} public String getSubText() {
return subText;
} public void setSubText(String subText) {
this.subText = subText;
btReduce.setText(subText);
}
}
4、在显示视图中添加该自定义View
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:asv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="数字通过按键可以加减: "
android:textSize="20sp">
</TextView> <com.ileevey.addsub.AddAndSubView
android:id="@+id/meter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
asv:addBackground="@drawable/selector_add"
asv:initValue="0"
asv:maxValue="10"
asv:minValue="-10"
asv:subBackground="@drawable/selector_sub"/> </LinearLayout>
效果如下图:

Android 自定义数字加减器的更多相关文章
- 我的Android进阶之旅------>Android自定义View实现带数字的进度条(NumberProgressBar)
今天在Github上面看到一个来自于 daimajia所写的关于Android自定义View实现带数字的进度条(NumberProgressBar)的精彩案例,在这里分享给大家一起来学习学习!同时感谢 ...
- Android自定义View4——统计图View
1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...
- (转)[原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- Android 自定义View合集
自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...
- [原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- Android自定义View
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View ...
- 带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变
带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变 转 https://blog.csdn.net/qq_30993595/article/details/78915115 近期有 ...
- android 自定义动画
android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...
- Android自定义View 画弧形,文字,并增加动画效果
一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类 B ...
随机推荐
- nginx中使用perl模块
转载自:http://www.netingcn.com/nginx-perl.html 如果对于一个绝大部分内容是静态的网站,只有极少数的地方需要动态显示,碰巧你又了解一点perl知识,那么nginx ...
- Qt精简编译方法总结
原文请看:http://blog.csdn.net/loaden/article/details/6061702 Qt如果采取默认编译安装,一般都要占用上G的空间.当初自己不想涉及Qt的一个原因,就是 ...
- python打印所有汉字
n=0 for ch in xrange(0x4e00, 0x9fa6): print unichr(ch), n = n+1 if(n%50==0): print '\n' print n
- MYSQL注入天书之导入导出介绍
Background-3 导入导出相关操作的讲解 load_file()导出文件 Load_file(file_name):读取文件并返回该文件的内容作为一个字符串. 使用条件: A.必须有权限读取并 ...
- Spring的安全机制
Spring Security:它提供全面的安全性解决方案,同时在Web请求和方法调用处理身份确认和授权,利用依赖注入和aop技术.主要名词: 1,安全拦截器:相当应用的一把锁,能够阻止对应用程序中保 ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- vijos p1777 引水入城(bfs+贪心)
引水入城 描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N行M列的矩形,其中每个格子都代表一座城市,每座城市都有一个海拔高度. 为了使 ...
- [Arc062] Painting Graphs with AtCoDeer
[Arc062] Painting Graphs with AtCoDeer Description 给定一张N点M边的无向图,每条边要染一个编号在1到K的颜色.你可以对一张染色了的图进行若干次操作, ...
- 浅谈分布式CAP定理
互联网发展到现在,由于数据量大.操作并发高等问题,大部分网站项目都采用分布式的架构.而分布式系统最大的特点数据分散,在不同网络节点在某些时刻(数据未同步完,数据丢失),数据会不一致. 在2000年,E ...
- 20172333 2017-2018-2 《Java程序设计》第3周学习总结
20172333 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 1.String类.Random类.Math类.NumberFormat类和DecimalF ...