自定义View加减

来源:互联网 发布:java date format 编辑:程序博客网 时间:2024/06/01 13:32

1.自定义View

import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.os.Build;import android.text.TextUtils;import android.util.AttributeSet;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;/** * Created by T_baby on 17/11/22. */public class NumberAddSubView extends LinearLayout implements View.OnClickListener {    private Button btn_sub;    private Button btn_add;    private TextView tv_num;    private Context mContext;    /**     * 设置默认值     */    private int value = 1;    private int minValue = 1;    private int maxValue = 5;    public NumberAddSubView(Context context) {        this(context, null);    }    public NumberAddSubView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)    public NumberAddSubView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        this.mContext = context;        initView(context);        //得到属性        if (attrs != null) {            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumberAddSub);            int value = a.getInt(R.styleable.NumberAddSub_value, 0);            setValue(value);            int maxValue = a.getInt(R.styleable.NumberAddSub_maxValue, 0);            setMaxValue(maxValue);            int minValue = a.getInt(R.styleable.NumberAddSub_minValue, 0);            setMinValue(minValue);            Drawable btnSubBackground = a.getDrawable(R.styleable.NumberAddSub_btnSubBackground);            if (btnSubBackground != null)                btn_sub.setBackground(btnSubBackground);            Drawable btnAddBackground = a.getDrawable(R.styleable.NumberAddSub_btnAddBackground);            if (btnAddBackground != null)                btn_sub.setBackground(btnAddBackground);            Drawable textViewBackground = a.getDrawable(R.styleable.NumberAddSub_textViewBackground);            if (textViewBackground != null)                tv_num.setBackground(textViewBackground);            a.recycle();        }    }    private void initView(Context context) {        //第三个参数:把当前View加载到NumberAddSubView控件上        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_num = (TextView) findViewById(R.id.tv_num);        btn_sub.setOnClickListener(this);        btn_add.setOnClickListener(this);    }    public int getValue() {        String val = tv_num.getText().toString();        if (!TextUtils.isEmpty(val)) {            value = Integer.parseInt(val);        }        return value;    }    public void setValue(int value) {        this.value = value;        tv_num.setText(value + "");    }    public int getMinValue() {        return minValue;    }    public void setMinValue(int minValue) {        this.minValue = minValue;    }    public int getMaxValue() {        return maxValue;    }    public void setMaxValue(int maxValue) {        this.maxValue = maxValue;    }    @Override    public void onClick(View v) {        if (v.getId() == R.id.btn_sub) {//            Toast.makeText(mContext,"减",Toast.LENGTH_SHORT).show();            subNum();            if (onButtonClickListenter != null) {                onButtonClickListenter.onButtonSubClick(v, value);            }        } else if (v.getId() == R.id.btn_add) {//            Toast.makeText(mContext,"加",Toast.LENGTH_SHORT).show();            addNum();            if (onButtonClickListenter != null) {                onButtonClickListenter.onButtonAddClick(v, value);            }        }    }    /**     * 减少数据     */    private void subNum() {        if (value > minValue) {            value = value - 1;            tv_num.setText(value + "");        }    }    /**     * 添加数据     */    private void addNum() {        if (value < maxValue) {            value = value + 1;            tv_num.setText(value + "");        }    }    public interface OnButtonClickListenter {        /**         * 当增加按钮被点击的时候回调该方法         *         * @param view         * @param value         */        public void onButtonAddClick(View view, int value);        /**         * 当减少按钮被点击的时候回调这个方法         *         * @param view         * @param value         */        public void onButtonSubClick(View view, int value);    }    private NumberAddSubView.OnButtonClickListenter onButtonClickListenter;    public void setOnButtonClickListenter(NumberAddSubView.OnButtonClickListenter onButtonClickListenter) {        this.onButtonClickListenter = onButtonClickListenter;    }}2.自定义属性
<declare-styleable name="NumberAddSub">    <attr name="value" format="integer|reference"/>    <attr name="minValue" format="integer|reference"/>    <attr name="maxValue" format="integer|reference"/>    <attr name="btnAddBackground" format="reference"/>    <attr name="btnSubBackground" format="reference"/>    <attr name="textViewBackground" format="reference"/></declare-styleable>
3.布局
 
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.bwie.day20.MainActivity"><com.bwie.day20.NumberAddSubView    android:id="@+id/nb_addsub_view"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    app:value="1"    app:minValue="1"    app:maxValue="5"    ></com.bwie.day20.NumberAddSubView></android.support.constraint.ConstraintLayout>
4.bg_btn_style_white.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_enabled="false">        <shape android:shape="rectangle">            <corners android:radius="2.0dp" />            <solid android:color="#7fd8d8d8" />            <stroke android:width="1.0dp" android:color="#dddddd" />        </shape>    </item>    <item android:state_pressed="true">        <shape android:shape="rectangle">            <corners android:radius="2.0dp" />            <solid android:color="#ffd8d8d8" />            <stroke android:width="1.0dp" android:color="#ddd" />        </shape>    </item>    <item>        <shape android:shape="rectangle">            <corners android:radius="2.0dp" />            <solid android:color="#ffffff" />            <stroke android:width="1.0dp" android:color="#dddddd" />        </shape>    </item></selector>
5.selector_number_add_sub.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <corners android:radius="2dp" />    <stroke        android:width="1dp"        android:color="#dddddd" />    <solid android:color="#FFFFFF" /></shape>

6.主要代码

package com.bwie.day20;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    private NumberAddSubView nb_addsub_view;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        nb_addsub_view = findViewById(R.id.nb_addsub_view);        nb_addsub_view.setOnButtonClickListenter(new NumberAddSubView.OnButtonClickListenter() {            @Override            public void onButtonAddClick(View view, int value) {                Toast.makeText(MainActivity.this, "AddClick Vaule==" + value, Toast.LENGTH_SHORT).show();            }            @Override            public void onButtonSubClick(View view, int value) {                Toast.makeText(MainActivity.this, "SubClick Vaule==" + value, Toast.LENGTH_SHORT).show();            }        });    }}

原创粉丝点击