Paste_Image.png
創(chuàng)建自定義控件的布局文件number_add_sub_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/number_add_sub_view_selector"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="@dimen/value_10dp"
android:paddingRight="@dimen/value_10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textColor="@color/txt_gray_c"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_num_units"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="單位"
android:textColor="@color/txt_gray_c"
android:textSize="@dimen/value_14sp" />
</LinearLayout>
<View
android:layout_width="@dimen/value_1dp"
android:layout_height="match_parent"
android:background="@color/txt_gray_a"
android:layout_marginTop="@dimen/value_3dp"
android:layout_marginBottom="@dimen/value_3dp"
/>
<TextView
android:id="@+id/btn_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/value_10dp"
android:paddingRight="@dimen/value_10dp"
android:gravity="center"
android:text="-"
android:textColor="@color/txt_gray_d"
android:textSize="25sp" />
<TextView
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="@dimen/value_10dp"
android:paddingRight="@dimen/value_10dp"
android:text="+"
android:textColor="@color/txt_gray_d"
android:textSize="25sp" />
</LinearLayout>
布局中用到控件的選擇器请祖,整體布局的邊框線number_add_sub_view_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!--設(shè)置圓角-->
<corners android:radius="2dp"></corners>
<!--設(shè)置固定背景顏色-->
<solid android:color="#ffffff"></solid>
<!--設(shè)置邊框顏色-->
<stroke android:color="#dddddd" android:width="1dp"></stroke>
</shape>
按鈕控件的按下背景顏色選擇器btn_number_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--沒按下狀態(tài)-->
<item android:state_enabled="false">
<shape android:shape="rectangle">
<solid android:color="#7fd8d8d8"></solid>
</shape>
</item>
<!--按下狀態(tài)-->
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#ffd8d8d8"></solid>
</shape>
</item>
<!--默認狀態(tài)-->
<item >
<shape android:shape="rectangle">
<solid android:color="#ffffff"></solid>
</shape>
</item>
</selector>
自定義一個類編寫該布局文件的邏輯操作形成自定義的控件,布局的父控件是線性布局,所以新建一個類繼 承線性布局NumberAddSubView
步驟1:創(chuàng)建一個類,并且把布局與該類形成一個整體
View.inflate(context,R.layout.number_add_sub_view,this);
這一句代碼是關(guān)鍵署咽,里面的最后一個參數(shù)this就是把這個類和布局文件number_add_sub_view形成一個整體哪痰。
public class NumberAddSubView extends LinearLayout{
public NumberAddSubView(Context context) {
this(context,null);
}
public NumberAddSubView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public NumberAddSubView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//要讓布局和當前類形成一個整體
View view = View.inflate(context,R.layout.number_add_sub_view,this);
}
}
步驟二、控件的邏輯處理桐罕,并且定義接口回調(diào)響應(yīng)控件
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* 自定義數(shù)字加減控件
* Created by willkong on 2016/12/14.
*/
public class NumberAddSubView extends LinearLayout implements View.OnClickListener {
private Button btn_sub;
private Button btn_add;
private TextView tv_value;
//屬性監(jiān)聽
private int value = 1;//默認值
private int minValue = 1;//最小值
private int maxValue = 10;//最大值
public int getMaxValue() {
return maxValue;
}
public void setMaxValue(int maxValue) {
this.maxValue = maxValue;
}
public int getValue() {
String valueStr = tv_value.getText().toString().trim();//文本的內(nèi)容
if (!TextUtils.isEmpty(valueStr)){
value = Integer.valueOf(valueStr);
}
return value;
}
public void setValue(int value) {
this.value = value;
tv_value.setText(value+"");
}
public int getMinValue() {
return minValue;
}
public void setMinValue(int minValue) {
this.minValue = minValue;
}
public NumberAddSubView(Context context) {
this(context, null);
}
public NumberAddSubView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public NumberAddSubView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//要讓布局和當前類形成一個整體
View.inflate(context, R.layout.number_add_sub_view, this);
btn_sub = (Button) findViewById(R.id.btn_sub);
btn_add = (Button) findViewById(R.id.btn_add);
tv_value = (TextView) findViewById(R.id.tv_value);
getValue();//獲得當前值
//設(shè)置點擊事件
btn_add.setOnClickListener(this);
btn_sub.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_sub://減
subNumber();
if (listener != null){
listener.onButtonSub(v,value);
}
break;
case R.id.btn_add://加
addNumber();
if (listener != null){
listener.onButtonAdd(v,value);
}
break;
}
}
/**
* 減
*/
private void subNumber() {
if (value > minValue){
value -= 1;
}
setValue(value);
}
/**
* 加
*/
private void addNumber() {
if (value < maxValue){
value += 1;
}
setValue(value);
}
/**
* 監(jiān)聽數(shù)字增加減少控件
*/
public interface OnNumberClickListener{
/**
* 當減少按鈕被點擊的時候回調(diào)
* @param view
* @param value
*/
void onButtonSub(View view,int value);
/**
* 當增加按鈕被點擊的時候回調(diào)
* @param view
* @param value
*/
void onButtonAdd(View view,int value);
}
public OnNumberClickListener listener;
/**
* 設(shè)置監(jiān)聽數(shù)字按鈕
* @param listener
*/
public void setOnNumberClickListener(OnNumberClickListener listener){
this.listener = listener;
}
}
步驟三脉让、自定義控件屬性
新建一個attrs文件,名稱自定義如:number_add_sub_view_attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="NumberAddSubView">
<attr name="value" format="integer|reference"></attr>
<attr name="minValue" format="integer|reference"></attr>
<attr name="maxValue" format="integer|reference"></attr>
<attr name="NumberAddSubBackground" format="reference"></attr>
<attr name="NumberAddBackground" format="reference"></attr>
<attr name="NumberSubBackground" format="reference"></attr>
<attr name="NumberUnits" format="string|reference"></attr>
</declare-styleable>
</resources>
步驟四功炮、在自定義類中獲取屬性
package com.szputy.print.view;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.TintTypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.szputy.print.R;
/**
* 自定義數(shù)字加減控件
* @author zhangjy
* @time 2017/1/19 12:46
**/
public class NumberAddSubView extends LinearLayout implements View.OnClickListener{
private TextView btn_sub;
private TextView btn_add;
private TextView numUnits;
private TextView tv_value;
//屬性監(jiān)聽
private int value = 1;//默認值
private int minValue = 1;//最小值
private int maxValue = 10;//最大值
public NumberAddSubView(Context context) {
this(context,null);
}
public NumberAddSubView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public int getMaxValue() {
return maxValue;
}
public void setMaxValue(int maxValue) {
this.maxValue = maxValue;
}
public int getValue() {
String valueStr = tv_value.getText().toString().trim();//文本的內(nèi)容
if (!TextUtils.isEmpty(valueStr)){
value = Integer.valueOf(valueStr);
}
return value;
}
public void setValue(int value) {
this.value = value;
tv_value.setText(value+"");
}
public int getMinValue() {
return minValue;
}
public void setMinValue(int minValue) {
this.minValue = minValue;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public NumberAddSubView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//要讓布局和當前類形成一個整體
View.inflate(context, R.layout.number_add_sub_view, this);
btn_sub = (TextView) findViewById(R.id.btn_sub);
btn_add = (TextView) findViewById(R.id.btn_add);
numUnits = (TextView)findViewById(R.id.tv_num_units);
tv_value = (TextView) findViewById(R.id.tv_value);
getValue();//獲得當前值
//設(shè)置點擊事件
btn_add.setOnClickListener(this);
btn_sub.setOnClickListener(this);
//得到屬性
if (attrs != null) {
TintTypedArray typedArray = TintTypedArray.obtainStyledAttributes
(context, attrs, R.styleable.NumberAddSubView);
int value = typedArray.getInt(R.styleable.NumberAddSubView_value, 0);
if (value > 0) {
setValue(value);
}
int minValue = typedArray.getInt(R.styleable.NumberAddSubView_minValue, 0);
if (minValue > 0) {
setMinValue(minValue);
}
int maxValue = typedArray.getInt(R.styleable.NumberAddSubView_maxValue, 0);
if (maxValue > 0) {
setMaxValue(maxValue);
}
Drawable numberAddSubBackground = typedArray.getDrawable
(R.styleable.NumberAddSubView_NumberAddSubBackground);
if (numberAddSubBackground != null) {
setBackground(numberAddSubBackground);
}
Drawable numberAddBackground = typedArray.getDrawable
(R.styleable.NumberAddSubView_NumberAddBackground);
if (numberAddBackground != null) {
btn_add.setBackground(numberAddBackground);
}
Drawable numberSubBackground = typedArray.getDrawable
(R.styleable.NumberAddSubView_NumberSubBackground);
if (numberSubBackground != null) {
btn_sub.setBackground(numberSubBackground);
}
String numberUnits = typedArray.getString(R.styleable.NumberAddSubView_NumberUnits);
if (numberUnits != null) {
numUnits.setText(numberUnits);
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_sub://減
subNumber();
if (listener != null){
listener.onButtonSub(v,value);
}
break;
case R.id.btn_add://加
addNumber();
if (listener != null){
listener.onButtonAdd(v,value);
}
break;
}
}
/**
* 減
*/
private void subNumber() {
if (value > minValue){
value -= 1;
}
setValue(value);
}
/**
* 加
*/
private void addNumber() {
if (value < maxValue){
value += 1;
}
setValue(value);
}
/**
* 監(jiān)聽數(shù)字增加減少控件
*/
public interface OnNumberClickListener{
/**
* 當減少按鈕被點擊的時候回調(diào)
* @param view
* @param value
*/
void onButtonSub(View view,int value);
/**
* 當增加按鈕被點擊的時候回調(diào)
* @param view
* @param value
*/
void onButtonAdd(View view,int value);
}
public OnNumberClickListener listener;
/**
* 設(shè)置監(jiān)聽數(shù)字按鈕
* @param listener
*/
public void setOnNumberClickListener(OnNumberClickListener listener){
this.listener = listener;
}
}