自定义控件加减器

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="2dp"
    android:layout_marginLeft="10dp"
    android:layout_width="60dp"
    android:layout_height="30dp"
    android:layout_gravity="center_vertical"
    android:background="#99000000"
    android:gravity="center_vertical">

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

    <TextView
        android:text="1"
        android:layout_marginLeft="2dp"
        android:background="#ffffff"
        android:layout_weight="1"
        android:id="@+id/product_number_tv"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        />

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

</LinearLayout>

代码

package com.lichao.bwei.com.lichao20181120;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * date:2018/11/20
 * author:李超(li)
 * function:
 */
public class Add_Remove extends LinearLayout implements View.OnClickListener {
    private TextView sub_tv;
    private TextView product_number_tv;
    private TextView add_tv;
    private int i ;

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

    public Add_Remove(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Add_Remove(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View inflate = inflate(context, R.layout.add_remove, this);
        add_tv = inflate.findViewById(R.id.add_tv);
        product_number_tv = inflate.findViewById(R.id.product_number_tv);
        sub_tv = inflate.findViewById(R.id.sub_tv);
        add_tv.setOnClickListener(this);
        sub_tv.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_tv:
                i++;
                product_number_tv.setText(i+"");
                onNumberChangeListener.onNumberChange(i);
                break;
            case R.id.sub_tv:
                if (i>1){
                    i--;
                    product_number_tv.setText(i+"");
                   onNumberChangeListener.onNumberChange(i);
                }else {
                    Toast.makeText(getContext(),"不能在减少",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    public void setNum(int i){
        this.i = i;
        product_number_tv.setText(i+"");

    }
    public int getNum(int i){
        return i;
    }
    OnNumberChangeListener onNumberChangeListener;

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

    interface OnNumberChangeListener {
        void onNumberChange(int num);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值