自定義View加減

TTMMJJ99發表於2017-11-22

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();
            }
        });
    }
}

相關文章