package com.qh.***.sort2.addel;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.qh.***.sort2.R;
public class AddSumLayout extends LinearLayout {
private View rootView;
private TextView add, sub, number;
public AddSumLayout(Context context) {
this(context, null);
}
public AddSumLayout(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public AddSumLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
initListener();
}
private void initView(Context context) {
rootView = View.inflate(context, R.layout.layout_add_sum, this);
add = rootView.findViewById(R.id.tvAdd);
sub = rootView.findViewById(R.id.tvSub);
number = rootView.findViewById(R.id.tvNumber);
number.setText("1");
}
private void initListener() {
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//数据增加监听
add();
}
});
sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//数据减少监听
sub();
}
});
}
private void add() {
String cu = number.getText().toString();
int parseInt = Integer.parseInt(cu);
parseInt++;
setCurentCount(parseInt);
}
private void sub() {
String cu = number.getText().toString();
int parseInt = Integer.parseInt(cu);
if (parseInt > 1) {
parseInt--;
setCurentCount(parseInt);
} else {
Toast.makeText(getContext(), "不能再少了亲", Toast.LENGTH_SHORT).show();
}
}
public int getCurentCount() {
return Integer.parseInt(number.getText().toString());
}
public void setCurentCount(int num) {
number.setText(num + "");
if (onNumChangedListener != null) {
onNumChangedListener.onNumChanged(this, num);
}
}
public interface OnNumChangedListener {
void onNumChanged(View view, int curNum);
}
private OnNumChangedListener onNumChangedListener;
public void setOnNumChangedListener(OnNumChangedListener onNumChangedListener) {
this.onNumChangedListener = onNumChangedListener;
}
}
自定义购物车加减器
最新推荐文章于 2021-05-30 20:04:58 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)