package com.example.aason.zhusha46.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.aason.zhusha46.R;
public class AddView extends LinearLayout {
private TextView jia,jian;
private EditText count;
private int num=1;
public AddView(Context context) {
super(context);
}
public AddView(Context context,AttributeSet attrs) {
this(context, attrs,0);
}
public AddView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view =LayoutInflater.from(context).inflate(R.layout.myview,this,true);
jia = view.findViewById(R.id.jia);
jian = view.findViewById(R.id.jian);
count = view.findViewById(R.id.count);
count.setText("1");
jia.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
num++;
count.setText(num+"");
if (addCallBack != null) {
addCallBack.numCallBack(num);
}
}
});
jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
num--;
if (num == 0) {
num=1;
Toast.makeText(getContext(), "不能再减了", Toast.LENGTH_SHORT).show();
}
count.setText(num+"");
if (addCallBack != null) {
addCallBack.numCallBack(num);
}
}
});
}
public int getNum() {
return num;
}
private AddCallBack addCallBack;
public void setAddCallBack(AddCallBack addCallBack) {
this.addCallBack = addCallBack;
}
public interface AddCallBack{
void numCallBack(int num);
}
}