自定义加减器+点击事件

 
  
package com.example.lenovo.moniyuekao.ui;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lenovo.moniyuekao.R;

/**
 * date:2018/6/29 16:16
 
 * function:
 */
public class AddSubView extends LinearLayout {
    private ImageView mImageSub;
    private TextView mTextView;
    private ImageView mImageAdd;

    private int textnum;

    public int getTextnum() {
        String s = mTextView.getText().toString();
        Integer num = Integer.valueOf(s);
        return num;
    }

    public void setTextnum(int textnum) {
            mTextView.setText(textnum+"");
    }

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

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

    public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.add, this, true);
        initView(view);
        mImageAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = mTextView.getText().toString();
                Integer num = Integer.valueOf(s);
                num++;
                mTextView.setText(num+"");
                if (mOnAddItemClick!=null){
                    mOnAddItemClick.OnAddClick();
                }
            }
        });

        mImageSub.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = mTextView.getText().toString();
                Integer num = Integer.valueOf(s);
                if (num>1){
                    num--;
                }
                mTextView.setText(num+"");
                if (mOnAddItemClick!=null){
                    mOnAddItemClick.OnAddClick();
                }
            }
        });


    }

    private void initView(View view) {
        mImageSub = view.findViewById(R.id.imageSub);
        mTextView = view.findViewById(R.id.textView);
        mImageAdd = view.findViewById(R.id.imageAdd);
    }
    //j点击商品事件商品
    public interface OnAddItemClick{
        void OnAddClick();
    }

    private OnAddItemClick mOnAddItemClick;

    public void setmOnAddItemClick(OnAddItemClick onAddItemClick) {
        this.mOnAddItemClick = onAddItemClick;
    }

}




    

    

    

你可能感兴趣的:(自定义加减器+点击事件)