组合控件|组合控件 购物车 加减

package com.example.john.myshopping.view;

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.john.myshopping.R;

/**
* date:2018/6/17 18:32
* function:
*/
public class UI_Add extends LinearLayout {

private int MaxValue;
private ImageView mImgSub;
private TextView mTextNum;
private ImageView mImgAdd;

//设置库存
public int getMaxValue() {
return MaxValue;
}

public void setMaxValue(int maxValue) {
MaxValue = https://www.it610.com/article/maxValue;
}

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

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

public UI_Add(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = LayoutInflater.from(context).inflate(R.layout.addjian, this, true);
initView(view);

mImgSub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String s = mTextNum.getText().toString();
Integer num = Integer.valueOf(s);
if(num>0){
num--;
}

if (mGetDataTextView != null) {

mGetDataTextView.setOnClickJian();

mGetDataTextView.getTextViewDataToMun(num);
mTextNum.setText(num+""); //赋值
}
}
});


mImgAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String s = mTextNum.getText().toString();
Integer num = Integer.valueOf(s);
num++;
if (mGetDataTextView != null) {
mGetDataTextView.setOnClickAdd();

mGetDataTextView.getTextViewDataToMun(num);
mTextNum.setText(num+""); //赋值
}

}
});

}
//创建一个方法用于设置商品数量
public void setShoppingNum(int num){
mTextNum.setText(num+""); //赋值
}
//返回商品的数量
public int getShoppingNum(){
String num = mTextNum.getText().toString();
return Integer.parseInt(num);
}

private void initView(View view) {
mImgSub = view.findViewById(R.id.img_sub);
mTextNum = view.findViewById(R.id.text_num);
mImgAdd = view.findViewById(R.id.img_add);
}

//接口传值
public interface GetDataTextView {
void getTextViewDataToMun(Integer num);
void setOnClickAdd(); //加
void setOnClickJian(); //减
}
//声明接口对象
private GetDataTextView mGetDataTextView;
//提供外部调用的方法
public void setGetDataTextView(GetDataTextView getDataTextView) {
this.mGetDataTextView = getDataTextView;
}
}

    推荐阅读