public class AddView extends LinearLayout implements View.OnClickListener {
private TextView mRemvoe,mNumber,mAdd;
public AddView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.add_item,this);
initView();
}
public void setNumber(int number){
mNumber.setText(number+"");
}
//找控件
private void initView() {
mRemvoe=findViewById(R.id.remove);
mNumber=findViewById(R.id.number);
mAdd=findViewById(R.id.add);
mRemvoe.setOnClickListener(this);
mAdd.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int number = Integer.parseInt(mNumber.getText().toString());
switch (v.getId()){
case R.id.remove:
if (number>1){
number--;
if (callBack!=null){
callBack.setCount(number);
}
}else {
Toast.makeText(getContext(), "商品数量最少为1", Toast.LENGTH_SHORT).show();
}
break;
case R.id.add:
number++;
if (callBack!=null){
callBack.setCount(number);
}
break;
}
}
//接口回调改变count
public AddCallBack callBack;
public void setCallBack(AddCallBack back){
callBack=back;
}
public interface AddCallBack{
void setCount(int number);
}
}