1、自定义属性,控制加减按钮的大小
values文件夹下创建attrs文件
<declare-styleable name="AddSubView">
<attr name="add_sub_width" format="dimension"/>
<attr name="add_sub_height" format="dimension"/>
</declare-styleable>
2、定义边框背景 drawable中定义shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<stroke android:color="#666666" android:width="1px"/>
<size android:width="20dp" android:height="20dp"/>
</shape>
3、定义组合控件的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_sub"
android:layout_width="20dp"
android:text="-"
android:background="@drawable/add_sub_bg"
android:gravity="center"
android:layout_height="20dp" />
<TextView
android:id="@+id/tv_num"
android:layout_width="20dp"
android:text="1"
android:layout_weight="1"
android:background="@drawable/add_sub_bg"
android:gravity="center"
android:layout_height="20dp" />
<TextView
android:id="@+id/tv_add"
android:layout_width="20dp"
android:text="+"
android:background="@drawable/add_sub_bg"
android:gravity="center"
android:layout_height="20dp" />
</LinearLayout>
4、具体代码如下
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AddSubView extends LinearLayout implements View.OnClickListener {
private int width;
private int height;
private TextView mTvAdd;
private TextView mTvSub;
private TextView mTvNum;
private boolean setMaxNum = false;
private boolean setMinNum = false;
private int maxNum = 0;
private int minNum = 0;
private AddSubClickListener addSubClickListener;
public AddSubView(Context context) {
this(context, null);
}
public AddSubView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AddSubView);
width = typedArray.getDimensionPixelSize(R.styleable.AddSubView_add_sub_width, 20);
height = typedArray.getDimensionPixelSize(R.styleable.AddSubView_add_sub_height, 20);
typedArray.recycle();
initView(context);
}
private void initView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.add_sub_view, this);
mTvAdd = view.findViewById(R.id.tv_add);
mTvSub = view.findViewById(R.id.tv_sub);
mTvNum = view.findViewById(R.id.tv_num);
LayoutParams params = new LayoutParams(width, height);
mTvAdd.setLayoutParams(params);
mTvSub.setLayoutParams(params);
LayoutParams numParams = new LayoutParams(LayoutParams.WRAP_CONTENT, height);
numParams.weight = 1;
mTvNum.setLayoutParams(numParams);
mTvSub.setOnClickListener(this);
mTvAdd.setOnClickListener(this);
}
public void setMaxNum(int maxNum) {
setMaxNum = true;
this.maxNum = maxNum;
}
public void setMinNum(int minNum) {
setMinNum = true;
this.minNum = minNum;
}
public void setNum(int num) {
if (setMinNum) {
if (num <= minNum) {
mTvSub.setEnabled(false);
}
}
if (setMaxNum) {
if (num >= maxNum) {
mTvAdd.setEnabled(false);
}
}
String s = Integer.toString(num);
mTvNum.setText(s);
}
public int getNum(){
String s = mTvNum.getText().toString();
return Integer.parseInt(s);
}
public void setAddSubClickListener(AddSubClickListener addSubClickListener){
this.addSubClickListener = addSubClickListener;
}
@Override
public void onClick(View view) {
if (addSubClickListener != null) {
if (view.getId() == R.id.tv_add) {
String s = mTvNum.getText().toString();
int num = Integer.parseInt(s);
if (setMaxNum) {
if (num < maxNum) {
num++;
if (num == maxNum) {
mTvAdd.setEnabled(false);
}
}
} else {
num++;
}
if (!mTvSub.isEnabled()) {
mTvSub.setEnabled(true);
}
mTvNum.setText(Integer.toString(num));
addSubClickListener.addSubClickListener(num);
} else if (view.getId() == R.id.tv_sub) {
String s = mTvNum.getText().toString();
int num = Integer.parseInt(s);
if (setMinNum) {
if (num > minNum) {
num--;
if (num == minNum) {
mTvSub.setEnabled(false);
}
}
} else {
num--;
}
if (!mTvAdd.isEnabled()) {
mTvAdd.setEnabled(true);
}
mTvNum.setText(Integer.toString(num));
addSubClickListener.addSubClickListener(num);
}
}
}
public interface AddSubClickListener {
void addSubClickListener(int num);
}
}