该自定义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 自定义数字加减器的更多相关文章

  1. 我的Android进阶之旅------>Android自定义View实现带数字的进度条(NumberProgressBar)

    今天在Github上面看到一个来自于 daimajia所写的关于Android自定义View实现带数字的进度条(NumberProgressBar)的精彩案例,在这里分享给大家一起来学习学习!同时感谢 ...

  2. Android自定义View4——统计图View

    1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...

  3. (转)[原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  4. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

  5. [原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  6. Android自定义View

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View ...

  7. 带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变

    带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变 转 https://blog.csdn.net/qq_30993595/article/details/78915115   近期有 ...

  8. android 自定义动画

    android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...

  9. Android自定义View 画弧形,文字,并增加动画效果

    一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类   B ...

随机推荐

  1. tmpfs文件系统

    centos 7测试OK 创建挂载点,挂载 mkdir -p /run/testdirmount -nt tmpfs -o size=500m,mode=755 tmpfs /run/testdir

  2. PHP 边执行边输出

    <?php for ($i = 1; $i <= 5; $i++) { print "#$i 完毕<hr>"; sleep(1); print str_pa ...

  3. 【转载】AsyncTask源码分析

    原文地址:https://github.com/white37/AndroidSdkSourceAnalysis/blob/master/article/AsyncTask%E5%92%8CAsync ...

  4. 微信JSSDK分享功能实现

    <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> <scri ...

  5. ORACLE数据库创建动态表

    最近公司一个项目代码里的定时任务无法执行,查验代码良久,奈何代码过于老旧,开发人员换了一茬又一茬,现在都无法理清,故无奈只好到数据库里重新写存过,配置定时任务. 在写存过时,由于检测及安全性能要求,需 ...

  6. Mac下配置PHP支持GD库FreeType

    一句话脚本 curl -s http://php-osx.liip.ch/install.sh | bash -s 5.6 记得要FQ哦. 或者下面代码保存成.sh ,代码从http://php-os ...

  7. 【POJ 2154】 Color (置换、burnside引理)

    Color Description Beads of N colors are connected together into a circular necklace of N beads (N< ...

  8. HashMap的实现原理 HashMap底层实现,hashCode如何对应bucket?

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 数组和链表组合成的链表散列结构,通过hash算法,尽量将数组中的数据分布均匀,如果has ...

  9. Sd - Hibernate

    Sd - Hibernate 附:http://blog.csdn.net/ta8210/article/details/1582162 数据库连接池实现原理 http://hi.baidu.com/ ...

  10. POJ2234 Matches Game 尼姆博弈 博弈论

    http://poj.org/problem?id=2234 尼姆博弈(Nimm's Game) 指的是这样一个博弈游戏:有任意堆物品,每堆物品的个数是任意的,双方轮流从中取物品,每一次只能从一堆物品 ...