ublic class Zdy extends LinearLayout {
private TextView jia;
private TextView jian;
private EditText et_num;
private int num=1;
public Zdy(Context context) {
this(context,null);
}
public Zdy(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public Zdy(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(final Context context) {
View view = View.inflate(context, R.layout.addview,null);
jia = findViewById(R.id.jia);
jian = findViewById(R.id.jian);
et_num = findViewById(R.id.et_num);
et_num.setText("1");
jia.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
num = Integer.parseInt(et_num.getText().toString());
num++;
et_num.setText(num+"");
if (onAddLisener!=null){
onAddLisener.numLisener(num);
}
onAddLisener.numLisener(num);
}
});
jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
num = Integer.parseInt(et_num.getText().toString());
num--;
if (num==0){
num=1;
Toast.makeText(context,"不能再减了",Toast.LENGTH_SHORT).show();
return;
}
et_num.setText(num+"");
if (onAddLisener !=null){
onAddLisener.numLisener(num);
}
onAddLisener.numLisener(num);
}
});
}
private OnAddLisener onAddLisener;
public interface OnAddLisener{
void numLisener(int num);
}
public void setOnAddLisener(OnAddLisener onAddLisener) {
this.onAddLisener = onAddLisener;
}
}