Android 自定义数字加减器
该自定义View主要是实现一款效果不错的数字加减器的功能的,但是也可以自定义选择器的外观颜色等。
1、自定义View的布局(add_sub_view.xml)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@android:color/white" 6 android:orientation="horizontal"> 7 8 <Button 9 android:id="@+id/bt01" 10 android:layout_width="32dp" 11 android:layout_height="32dp" 12 android:scaleType="fitCenter" 13 android:textSize="18sp"/> 14 15 <TextView 16 android:id="@+id/et01" 17 android:layout_width="32dp" 18 android:layout_height="32dp" 19 android:enabled="false" 20 android:layout_gravity="center" 21 android:gravity="center" 22 android:textColor="#000000" 23 android:textSize="16sp"> 24 </TextView> 25 26 <Button 27 android:id="@+id/bt02" 28 android:layout_width="32dp" 29 android:layout_height="32dp" 30 android:scaleType="fitCenter" 31 android:textSize="18sp"/> 32 33 </LinearLayout>
2、定义该View的一些属性(attrs.xml)
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <declare-styleable name="AddAndSubView"> 4 <attr name="textColor" format="color"/> 5 <attr name="initValue" format="integer"/> 6 <attr name="maxValue" format="integer"/> 7 <attr name="minValue" format="integer"/> 8 <attr name="textSize" format="dimension"/> 9 <attr name="textFrameBackground" format="reference|color"/> 10 <attr name="addBackground" format="reference|color"/> 11 <attr name="subBackground" format="reference|color"/> 12 <attr name="textFrameWidth" format="dimension"/> 13 <attr name="addWidth" format="dimension"/> 14 <attr name="subWidth" format="dimension"/> 15 <attr name="addText" format="string"/> 16 <attr name="subText" format="string"/> 17 </declare-styleable> 18 19 </resources>
3、定义该View的Java类(AddAndSubView.java)
1 package com.ileevey.addsub; 2 3 import android.content.Context; 4 import android.content.res.Resources; 5 import android.content.res.TypedArray; 6 import android.graphics.drawable.ColorDrawable; 7 import android.graphics.drawable.Drawable; 8 import android.util.AttributeSet; 9 import android.view.LayoutInflater; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.LinearLayout; 13 import android.widget.TextView; 14 15 public class AddAndSubView extends LinearLayout{ 16 17 /** 显示文本 */ 18 private TextView mTextView; 19 /** 增加按钮 */ 20 private Button btAdd; 21 /** 减少按钮 */ 22 private Button btReduce; 23 /** 显示文本的长宽 */ 24 private int textFrameWidth; 25 /** 显示文本及button中文字的颜色 */ 26 private int textColor; 27 /** 初始值 */ 28 private int initValue; 29 /** 最大值 */ 30 private int maxValue; 31 /** 最小值 */ 32 private int minValue; 33 /** 显示文本及button中文字的大小 */ 34 private int textSize; 35 /** 显示文本的背景 */ 36 private Drawable textFrameBackground; 37 /** 增加按钮的背景 */ 38 private Drawable addBackground; 39 /** 减少按钮的背景 */ 40 private Drawable subBackground; 41 /** 增加按钮的大小 */ 42 private int addWidth; 43 /** 减少按钮的大小 */ 44 private int subWidth; 45 /** 增加按钮中的文本 */ 46 private String addText; 47 /** 减少按钮中的文本 */ 48 private String subText; 49 50 public AddAndSubView(Context context, AttributeSet attrs) { 51 super(context, attrs); 52 initWidget(context); 53 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AddAndSubView); 54 textColor = a.getColor(R.styleable.AddAndSubView_textColor, getResources().getColor(android.R.color.black)); 55 textSize = a.getDimensionPixelSize(R.styleable.AddAndSubView_textSize, 16); 56 textFrameBackground = a.getDrawable(R.styleable.AddAndSubView_textFrameBackground); 57 textFrameWidth = a.getDimensionPixelSize(R.styleable.AddAndSubView_textFrameWidth, 48); 58 addBackground = a.getDrawable(R.styleable.AddAndSubView_addBackground); 59 subBackground = a.getDrawable(R.styleable.AddAndSubView_subBackground); 60 initValue = a.getInt(R.styleable.AddAndSubView_initValue, 0); 61 maxValue = a.getInt(R.styleable.AddAndSubView_maxValue, 1000000000); 62 minValue = a.getInt(R.styleable.AddAndSubView_minValue, -1000000000); 63 addWidth = a.getDimensionPixelSize(R.styleable.AddAndSubView_addWidth, 48); 64 subWidth = a.getDimensionPixelSize(R.styleable.AddAndSubView_subWidth, 48); 65 addText = a.getString(R.styleable.AddAndSubView_addText); 66 subText = a.getString(R.styleable.AddAndSubView_subText); 67 setAddBackground(addBackground); 68 setAddText(addText); 69 setAddWidth(addWidth); 70 setInitValue(initValue); 71 setMaxValue(maxValue); 72 setMinValue(minValue); 73 setSubBackground(subBackground); 74 setSubText(subText); 75 setSubWidth(subWidth); 76 setTextColor(textColor); 77 setTextFrameBackground(textFrameBackground); 78 setTextFrameWidth(textFrameWidth); 79 setTextSize(textSize); 80 a.recycle(); 81 } 82 83 84 protected void onFinishInflate() { 85 super.onFinishInflate(); 86 addListener(); 87 88 } 89 90 public void initWidget(Context context){ 91 LayoutInflater.from(context).inflate(R.layout.add_sub_view, this); 92 mTextView = (TextView)findViewById(R.id.et01); 93 btAdd = (Button)findViewById(R.id.bt01); 94 btReduce = (Button)findViewById(R.id.bt02); 95 } 96 97 public void addListener(){ 98 btAdd.setOnClickListener(new OnClickListener() { 99 100 public void onClick(View v) { 101 102 int num = Integer.valueOf(mTextView.getText().toString()); 103 num++; 104 if (num >= maxValue+1) 105 return; 106 mTextView.setText(Integer.toString(num)); 107 } 108 }); 109 110 btReduce.setOnClickListener(new OnClickListener() { 111 112 @Override 113 public void onClick(View v) { 114 int num = Integer.valueOf(mTextView.getText().toString()); 115 num--; 116 if (num <= minValue-1) 117 return; 118 mTextView.setText(Integer.toString(num)); 119 } 120 }); 121 } 122 123 124 public int getTextFrameWidth() { 125 return textFrameWidth; 126 } 127 128 129 public void setTextFrameWidth(int textFrameWidth) { 130 this.textFrameWidth = textFrameWidth; 131 mTextView.setWidth(textFrameWidth); 132 mTextView.setHeight(textFrameWidth); 133 } 134 135 136 public int getTextColor() { 137 return textColor; 138 } 139 140 141 public void setTextColor(int textColor) { 142 this.textColor = textColor; 143 mTextView.setTextColor(textColor); 144 btAdd.setTextColor(textColor); 145 btReduce.setTextColor(textColor); 146 } 147 148 149 public int getInitValue() { 150 return initValue; 151 } 152 153 154 public void setInitValue(int initValue) { 155 this.initValue = initValue; 156 mTextView.setText(String.valueOf(initValue)); 157 } 158 159 160 public int getMaxValue() { 161 return maxValue; 162 } 163 164 165 public void setMaxValue(int maxValue) { 166 this.maxValue = maxValue; 167 } 168 169 170 public int getMinValue() { 171 return minValue; 172 } 173 174 175 public void setMinValue(int minValue) { 176 this.minValue = minValue; 177 } 178 179 180 public int getTextSize() { 181 return textSize; 182 } 183 184 185 public void setTextSize(int textSize) { 186 this.textSize = textSize; 187 mTextView.setTextSize(textSize); 188 } 189 190 191 public Drawable getTextFrameBackground() { 192 return textFrameBackground; 193 } 194 195 196 public void setTextFrameBackground(Drawable textFrameBackground) { 197 this.textFrameBackground = textFrameBackground; 198 mTextView.setBackgroundDrawable(textFrameBackground); 199 } 200 201 202 public Drawable getAddBackground() { 203 return addBackground; 204 } 205 206 207 public void setAddBackground(Drawable addBackground) { 208 this.addBackground = addBackground; 209 Resources res = getResources(); 210 int color = res.getColor(android.R.color.darker_gray); 211 Drawable drawable = new ColorDrawable(color); 212 btAdd.setBackgroundDrawable(addBackground==null?drawable:addBackground); 213 } 214 215 216 public Drawable getSubBackground() { 217 return subBackground; 218 } 219 220 221 public void setSubBackground(Drawable subBackground) { 222 this.subBackground = subBackground; 223 Resources res = getResources(); 224 int color = res.getColor(android.R.color.darker_gray); 225 Drawable drawable = new ColorDrawable(color); 226 btReduce.setBackgroundDrawable(subBackground==null?drawable:subBackground); 227 } 228 229 230 public int getAddWidth() { 231 return addWidth; 232 } 233 234 235 public void setAddWidth(int addWidth) { 236 this.addWidth = addWidth; 237 btAdd.setWidth(addWidth); 238 btAdd.setHeight(addWidth); 239 } 240 241 242 public int getSubWidth() { 243 return subWidth; 244 } 245 246 247 public void setSubWidth(int subWidth) { 248 this.subWidth = subWidth; 249 btReduce.setWidth(subWidth); 250 btReduce.setHeight(subWidth); 251 } 252 253 254 public String getAddText() { 255 return addText; 256 } 257 258 259 public void setAddText(String addText) { 260 this.addText = addText; 261 btAdd.setText(addText); 262 } 263 264 265 public String getSubText() { 266 return subText; 267 } 268 269 270 public void setSubText(String subText) { 271 this.subText = subText; 272 btReduce.setText(subText); 273 } 274 }
4、在显示视图中添加该自定义View
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:asv="http://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="@android:color/white" 7 android:orientation="vertical"> 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_margin="16dp" 13 android:text="数字通过按键可以加减: " 14 android:textSize="20sp"> 15 </TextView> 16 17 <com.ileevey.addsub.AddAndSubView 18 android:id="@+id/meter" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:layout_gravity="center" 22 asv:addBackground="@drawable/selector_add" 23 asv:initValue="0" 24 asv:maxValue="10" 25 asv:minValue="-10" 26 asv:subBackground="@drawable/selector_sub"/> 27 28 </LinearLayout>
效果如下图: