1. 程式人生 > >自定義加減控制元件

自定義加減控制元件

public class MyAddSubView extends LinearLayout implements View.OnClickListener {

    private int number = 1;
    private TextView tv_sub;
    private TextView tv_number;
    private TextView tv_add;

    public MyAddSubView(Context context) {
        this(context, null);
    }

    public MyAddSubView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyAddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        View view = inflate(context, R.layout.add_remove, this);
        tv_sub = view.findViewById(R.id.tv_sub);
        tv_number = view.findViewById(R.id.tv_number);
        tv_add = view.findViewById(R.id.tv_add);

        tv_sub.setOnClickListener(this);
        tv_add.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_sub:
                if (number > 1){
                    --number;
                    tv_number.setText(number + "");
                    if (onNumberChangeListener != null){
                        onNumberChangeListener.onNumberChange(number);
                    }
                }else {
                    Toast.makeText(getContext(), "不能再少了!", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.tv_add:
                ++number;
                tv_number.setText(number + "");
                if (onNumberChangeListener != null){
                    onNumberChangeListener.onNumberChange(number);
                }
                break;
        }
    }

    public int getNumber(){
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
        tv_number.setText(number+"");
    }

    interface OnNumberChangeListener {
        void onNumberChange(int num);
    }

    public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }

    private OnNumberChangeListener onNumberChangeListener;


}

.add_remove.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="60dp"
    android:layout_height="30dp"
    android:layout_gravity="center_vertical">

    <TextView
        android:background="#dedbdb"
        android:id="@+id/tv_sub"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="-"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv_number"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="1"
        />

    <TextView
        android:background="#dedbdb"
        android:id="@+id/tv_add"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="+"
        android:textSize="16sp" />

</LinearLayout>