带有增加与减少按钮的数量选择控件
用法:
自定义
属性:
app:qv_addButtonBackground="color|drawable"app:qv_addButtonText="string"
app:qv_addButtonTextColor="color"
app:qv_removeButtonBackground="color|drawable"
app:qv_removeButtonText="string"
app:qv_removeButtonTextColor="color"
app:qv_quantityBackground="color|drawable"
app:qv_quantityTextColor="color"
app:qv_quantity="integer"
app:qv_quantityPadding="dimension"
app:qv_maxQuantity="integer"
app:qv_minQuantity="integer"
import android.annotation.TargetApi;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Quantity view to add and remove quantities
*/
public class QuantityView extends LinearLayout implements View.OnClickListener {
private Drawable quantityBackground, addButtonBackground, removeButtonBackground;
private String addButtonText, removeButtonText;
private int quantity, maxQuantity, minQuantity;
private int quantityPadding;
private int quantityTextColor, addButtonTextColor, removeButtonTextColor;
private Button mButtonAdd, mButtonRemove;
private TextView mTextViewQuantity;
public interface OnQuantityChangeListener {
void onQuantityChanged(int newQuantity, boolean programmatically);
void onLimitReached();
}
private OnQuantityChangeListener onQuantityChangeListener;
public QuantityView(Context context) {
super(context);
init(null, 0);
}
public QuantityView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public QuantityView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void init(AttributeSet attrs, int defStyle) {
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.QuantityView, defStyle, 0);
addButtonText = getResources().getString(R.string.qv_add);
if (a.hasValue(R.styleable.QuantityView_qv_addButtonText)) {
addButtonText = a.getString(R.styleable.QuantityView_qv_addButtonText);
}
addButtonBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_btn_selector);
if (a.hasValue(R.styleable.QuantityView_qv_addButtonBackground)) {
addButtonBackground = a.getDrawable(R.styleable.QuantityView_qv_addButtonBackground);
}
addButtonTextColor = a.getColor(R.styleable.QuantityView_qv_addButtonTextColor, Color.BLACK);
removeButtonText = getResources().getString(R.string.qv_remove);
if (a.hasValue(R.styleable.QuantityView_qv_removeButtonText)) {
removeButtonText = a.getString(R.styleable.QuantityView_qv_removeButtonText);
}
removeButtonBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_btn_selector);
if (a.hasValue(R.styleable.QuantityView_qv_removeButtonBackground)) {
removeButtonBackground = a.getDrawable(R.styleable.QuantityView_qv_removeButtonBackground);
}
removeButtonTextColor = a.getColor(R.styleable.QuantityView_qv_removeButtonTextColor, Color.BLACK);
quantity = a.getInt(R.styleable.QuantityView_qv_quantity, 0);
maxQuantity = a.getInt(R.styleable.QuantityView_qv_maxQuantity, Integer.MAX_VALUE);
minQuantity = a.getInt(R.styleable.QuantityView_qv_minQuantity, 0);
quantityPadding = (int) a.getDimension(R.styleable.QuantityView_qv_quantityPadding, pxFromDp(24));
quantityTextColor = a.getColor(R.styleable.QuantityView_qv_quantityTextColor, Color.BLACK);
quantityBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_bg_selector);
if (a.hasValue(R.styleable.QuantityView_qv_quantityBackground)) {
quantityBackground = a.getDrawable(R.styleable.QuantityView_qv_quantityBackground);
}
a.recycle();
int dp10 = pxFromDp(10);
mButtonAdd = new Button(getContext());
mButtonAdd.setGravity(Gravity.CENTER);
mButtonAdd.setPadding(dp10, dp10, dp10, dp10);
mButtonAdd.setMinimumHeight(0);
mButtonAdd.setMinimumWidth(0);
mButtonAdd.setMinHeight(0);
mButtonAdd.setMinWidth(0);
setAddButtonBackground(addButtonBackground);
setAddButtonText(addButtonText);
setAddButtonTextColor(addButtonTextColor);
mButtonRemove = new Button(getContext());
mButtonRemove.setGravity(Gravity.CENTER);
mButtonRemove.setPadding(dp10, dp10, dp10, dp10);
mButtonRemove.setMinimumHeight(0);
mButtonRemove.setMinimumWidth(0);
mButtonRemove.setMinHeight(0);
mButtonRemove.setMinWidth(0);
setRemoveButtonBackground(removeButtonBackground);
setRemoveButtonText(removeButtonText);
setRemoveButtonTextColor(removeButtonTextColor);
mTextViewQuantity = new TextView(getContext());
mTextViewQuantity.setGravity(Gravity.CENTER);
setQuantityTextColor(quantityTextColor);
setQuantity(quantity);
setQuantityBackground(quantityBackground);
setQuantityPadding(quantityPadding);
setOrientation(HORIZONTAL);
addView(mButtonRemove, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(mTextViewQuantity, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
addView(mButtonAdd, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mButtonAdd.setOnClickListener(this);
mButtonRemove.setOnClickListener(this);
mTextViewQuantity.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == mButtonAdd) {
if (quantity + 1 > maxQuantity) {
if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached();
} else {
quantity += 1;
mTextViewQuantity.setText(String.valueOf(quantity));
if (onQuantityChangeListener != null)
onQuantityChangeListener.onQuantityChanged(quantity, false);
}
} else if (v == mButtonRemove) {
if (quantity - 1 = Build.VERSION_CODES.JELLY_BEAN) {
mTextViewQuantity.setBackground(quantityBackground);
} else {
mTextViewQuantity.setBackgroundDrawable(quantityBackground);
}
}
public Drawable getAddButtonBackground() {
return addButtonBackground;
}
public void setAddButtonBackground(Drawable addButtonBackground) {
this.addButtonBackground = addButtonBackground;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mButtonAdd.setBackground(addButtonBackground);
} else {
mButtonAdd.setBackgroundDrawable(addButtonBackground);
}
}
public Drawable getRemoveButtonBackground() {
return removeButtonBackground;
}
public void setRemoveButtonBackground(Drawable removeButtonBackground) {
this.removeButtonBackground = removeButtonBackground;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mButtonRemove.setBackground(removeButtonBackground);
} else {
mButtonRemove.setBackgroundDrawable(removeButtonBackground);
}
}
public String getAddButtonText() {
return addButtonText;
}
public void setAddButtonText(String addButtonText) {
this.addButtonText = addButtonText;
mButtonAdd.setText(addButtonText);
}
public String getRemoveButtonText() {
return removeButtonText;
}
public void setRemoveButtonText(String removeButtonText) {
this.removeButtonText = removeButtonText;
mButtonRemove.setText(removeButtonText);
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
boolean limitReached = false;
if (quantity > maxQuantity) {
quantity = maxQuantity;
limitReached = true;
if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached();
}
if (quantity
qv_dialog_changequantity.xml
attr.xml
string.xml
+ -
drawable:
qv_bg_selector.xml
-
-
-
qv_btn_selector.xml
-
-
-
- 大小: 25.6 KB
- 查看图片附件
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net