首先我们需要的是 加(botton) 减 (botton) 输入数量(editText) 【这里我还设置了最大值】
然后对 加减按钮设置监听,点击对editText的内容进行修改。
布局代码:
//android:showDividers="middle"在每个子View间加入分隔线
再写我们需要在调用这个自定义View的时候设置它的属性【res -> values -> attrs.xml】写入
最后我们写自定义的LinearLayout,代码如下:
import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.support.annotation.Nullable;import android.text.Editable;import android.text.TextWatcher;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;import cn.jhc.startdemo.R;/** * Created by Administrator on 2017/10/14. */public class AddDeleteCount extends LinearLayout implements View.OnClickListener, TextWatcher { private int max, btnWidth, edtWidth, addOrDeleteBg; private EditText edt_count; //edit控件是否可以修改 private boolean IsEdtCountEdited = false; public AddDeleteCount(Context context) { this(context, null); } public AddDeleteCount(Context context, @Nullable AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.add_or_delete, this); TextView add = findViewById(R.id.add); add.setOnClickListener(this); TextView delete = findViewById(R.id.delete); delete.setOnClickListener(this); edt_count = findViewById(R.id.count); edt_count.addTextChangedListener(this); if (attrs != null) { TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.AddDeleteCount); max = array.getInt(R.styleable.AddDeleteCount_maxCount, -1); btnWidth = array.getDimensionPixelOffset(R.styleable.AddDeleteCount_btnWidth, 100); edtWidth = array.getDimensionPixelOffset(R.styleable.AddDeleteCount_editWidth, 80); addOrDeleteBg = array.getColor(R.styleable.AddDeleteCount_AddDeleteBackgroungColor, Color.GRAY); IsEdtCountEdited = array.getBoolean(R.styleable.AddDeleteCount_isEditCount, false); array.recycle(); } edt_count.setEnabled(IsEdtCountEdited); LayoutParams params = new LayoutParams(btnWidth, ViewGroup.LayoutParams.MATCH_PARENT); add.setLayoutParams(params); delete.setLayoutParams(params); LayoutParams params1 = new LayoutParams(edtWidth, ViewGroup.LayoutParams.MATCH_PARENT); edt_count.setLayoutParams(params1); add.setBackgroundColor(addOrDeleteBg); delete.setBackgroundColor(addOrDeleteBg); } @Override public void onClick(View view) { int currentCount = Integer.parseInt(edt_count.getText().toString()); switch (view.getId()) { case R.id.add: if (currentCount < max) { edt_count.setText(String.valueOf(currentCount + 1)); } break; case R.id.delete: if (currentCount > 1) { edt_count.setText(String.valueOf(currentCount - 1)); } break; } } @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (edt_count.getText().toString().isEmpty()) { return; } else { if (Integer.parseInt(edt_count.getText().toString()) > max) { edt_count.setText(String.valueOf(max)); } } } public void setEdtCountEdited(boolean edtCountEdited) { edt_count.setEnabled(edtCountEdited); } public int getCount() { if (!edt_count.getText().toString().isEmpty()) { return Integer.parseInt(edt_count.getText().toString()); } return 1; }}
最后在MainActivity布局中调用: