- 浏览: 5777510 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (890)
- WindowsPhone (0)
- android (88)
- android快速迭代 (17)
- android基础 (34)
- android进阶 (172)
- android高级 (0)
- android拾遗 (85)
- android动画&效果 (68)
- Material Design (13)
- LUA (5)
- j2me (32)
- jQuery (39)
- spring (26)
- hibernate (20)
- struts (26)
- tomcat (9)
- javascript+css+html (62)
- jsp+servlet+javabean (14)
- java (37)
- velocity+FCKeditor (13)
- linux+批处理 (9)
- mysql (19)
- MyEclipse (9)
- ajax (7)
- wap (8)
- j2ee+apache (24)
- 其他 (13)
- phonegap (35)
最新评论
-
Memories_NC:
本地lua脚本终于执行成功了,虽然不是通过redis
java中调用lua脚本语言1 -
ZHOU452840622:
大神://处理返回的接收状态 这个好像没有监听到 遇 ...
android 发送短信的两种方式 -
PXY:
拦截部分地址,怎么写的for(int i=0;i<lis ...
判断是否登录的拦截器SessionFilter -
maotou1988:
Android控件之带清空按钮(功能)的AutoComplet ...
自定义AutoCompleteTextView -
yangmaolinpl:
希望有表例子更好。。。,不过也看明白了。
浅谈onInterceptTouchEvent、onTouchEvent与onTouch
带有增加与减少按钮的数量选择控件
用法:
<me.himanshusoni.quantityview.QuantityView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/quantityView_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:qv_quantity="10" />
自定义
属性:
qv_dialog_changequantity.xml
attr.xml
string.xml
drawable:
qv_bg_selector.xml
qv_btn_selector.xml
用法:
<me.himanshusoni.quantityview.QuantityView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/quantityView_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:qv_quantity="10" />
自定义
属性:
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 < minQuantity) { if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached(); } else { quantity -= 1; mTextViewQuantity.setText(String.valueOf(quantity)); if (onQuantityChangeListener != null) onQuantityChangeListener.onQuantityChanged(quantity, false); } } else if (v == mTextViewQuantity) { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle("Change Quantity"); View inflate = LayoutInflater.from(getContext()).inflate(R.layout.qv_dialog_changequantity, null, false); final EditText et = (EditText) inflate.findViewById(R.id.qv_et_change_qty); et.setText(String.valueOf(quantity)); builder.setView(inflate); builder.setPositiveButton("Change", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String newQuantity = et.getText().toString(); if (isNumber(newQuantity)) { int intNewQuantity = Integer.parseInt(newQuantity); setQuantity(intNewQuantity); } } }).setNegativeButton("Cancel", null); builder.show(); } } public OnQuantityChangeListener getOnQuantityChangeListener() { return onQuantityChangeListener; } public void setOnQuantityChangeListener(OnQuantityChangeListener onQuantityChangeListener) { this.onQuantityChangeListener = onQuantityChangeListener; } public Drawable getQuantityBackground() { return quantityBackground; } public void setQuantityBackground(Drawable quantityBackground) { this.quantityBackground = quantityBackground; if (Build.VERSION.SDK_INT >= 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 < minQuantity) { quantity = minQuantity; limitReached = true; if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached(); } if (!limitReached && onQuantityChangeListener != null) onQuantityChangeListener.onQuantityChanged(quantity, true); this.quantity = quantity; mTextViewQuantity.setText(String.valueOf(this.quantity)); } public int getMaxQuantity() { return maxQuantity; } public void setMaxQuantity(int maxQuantity) { this.maxQuantity = maxQuantity; } public int getMinQuantity() { return minQuantity; } public void setMinQuantity(int minQuantity) { this.minQuantity = minQuantity; } public int getQuantityPadding() { return quantityPadding; } public void setQuantityPadding(int quantityPadding) { this.quantityPadding = quantityPadding; mTextViewQuantity.setPadding(quantityPadding, 0, quantityPadding, 0); } public int getQuantityTextColor() { return quantityTextColor; } public void setQuantityTextColor(int quantityTextColor) { this.quantityTextColor = quantityTextColor; mTextViewQuantity.setTextColor(quantityTextColor); } public void setQuantityTextColorRes(int quantityTextColorRes) { this.quantityTextColor = ContextCompat.getColor(getContext(), quantityTextColorRes); mTextViewQuantity.setTextColor(quantityTextColor); } public int getAddButtonTextColor() { return addButtonTextColor; } public void setAddButtonTextColor(int addButtonTextColor) { this.addButtonTextColor = addButtonTextColor; mButtonAdd.setTextColor(addButtonTextColor); } public void setAddButtonTextColorRes(int addButtonTextColorRes) { this.addButtonTextColor = ContextCompat.getColor(getContext(), addButtonTextColorRes); mButtonAdd.setTextColor(addButtonTextColor); } public int getRemoveButtonTextColor() { return removeButtonTextColor; } public void setRemoveButtonTextColor(int removeButtonTextColor) { this.removeButtonTextColor = removeButtonTextColor; mButtonRemove.setTextColor(removeButtonTextColor); } public void setRemoveButtonTextColorRes(int removeButtonTextColorRes) { this.removeButtonTextColor = ContextCompat.getColor(getContext(), removeButtonTextColorRes); mButtonRemove.setTextColor(removeButtonTextColor); } private int dpFromPx(final float px) { return (int) (px / getResources().getDisplayMetrics().density); } private int pxFromDp(final float dp) { return (int) (dp * getResources().getDisplayMetrics().density); } private boolean isNumber(String string) { try { Integer.parseInt(string); return true; } catch (Exception e) { return false; } } }
qv_dialog_changequantity.xml
<?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" android:orientation="vertical"> <EditText android:id="@+id/qv_et_change_qty" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:inputType="number" /> </LinearLayout>
attr.xml
<resources> <declare-styleable name="QuantityView"> <attr name="qv_addButtonText" format="string" /> <attr name="qv_addButtonBackground" format="color|reference" /> <attr name="qv_addButtonTextColor" format="color" /> <attr name="qv_removeButtonText" format="string" /> <attr name="qv_removeButtonBackground" format="color|reference" /> <attr name="qv_removeButtonTextColor" format="color" /> <attr name="qv_quantity" format="integer" /> <attr name="qv_quantityBackground" format="color|reference" /> <attr name="qv_quantityTextColor" format="color" /> <attr name="qv_quantityPadding" format="dimension" /> <attr name="qv_maxQuantity" format="integer" /> <attr name="qv_minQuantity" format="integer" /> </declare-styleable> </resources>
string.xml
<string name="qv_add">+</string> <string name="qv_remove">-</string>
drawable:
qv_bg_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="#ddd" /> </shape> </item> <item android:state_selected="true"> <shape android:shape="rectangle"> <solid android:color="#ddd" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#eee" /> </shape> </item> </selector>
qv_btn_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="#ccc" /> </shape> </item> <item android:state_selected="true"> <shape android:shape="rectangle"> <solid android:color="#ccc" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#ddd" /> </shape> </item> </selector>
发表评论
-
NestedScrollView滚动到顶部固定子View悬停挂靠粘在顶端
2018-10-31 20:45 6808网上有一个StickyScrollView,称之为粘性Scro ... -
自定义Behavior实现AppBarLayout越界弹性效果
2017-03-31 09:33 10233一、继承AppBarLayout.Beha ... -
Android - 一种相似图片搜索算法的实现
2017-03-31 09:33 2569算法 缩小尺寸。 将图片缩小到8x8的尺寸,总共64个 ... -
使用SpringAnimation实现带下拉弹簧动画的 ScrollView
2017-03-30 11:30 2792在刚推出的 Support Library 25.3.0 里面 ... -
Android为应用添加角标(Badge)
2017-03-30 11:21 61031.需求简介 角标是什么意思呢? 看下图即可明了: 可 ... -
Android端与笔记本利用局域网进行FTP通信
2017-03-23 10:17 925先看图 打开前: 打开后: Activity类 ... -
PorterDuffColorFilter 在项目中的基本使用
2017-03-03 10:58 1300有时候标题栏会浮在内容之上,而内容会有颜色的变化,这时候就要求 ... -
ColorAnimationView 实现了滑动Viewpager 时背景色动态变化的过渡效果
2017-02-24 09:41 2171用法在注释中: import android.anima ... -
迷你轻量级全方向完美滑动处理侧滑控件SlideLayout
2017-01-16 16:53 2550纯手工超级迷你轻量级全方向完美滑动处理侧滑控件(比官方 sup ... -
Effect
2017-01-05 09:57 0https://github.com/JetradarMobi ... -
动态主题库Colorful,容易地改变App的配色方案
2016-12-27 14:49 2519Colorful是一个动态主题库,允许您很容易地改变App的配 ... -
对视图的对角线切割DiagonalView
2016-12-27 14:23 1074提供对视图的对角线切割,具有很好的用户定制 基本用法 ... -
仿淘宝京东拖拽商品详情页上下滚动黏滞效果
2016-12-26 16:53 3421比较常用的效果,有现成的,如此甚好!:) import ... -
让任意view具有滑动效果的SlideUp
2016-12-26 09:26 1660基本的类,只有一个: import android.a ... -
AdvancedWebView
2016-12-21 09:44 16https://github.com/delight-im/A ... -
可设置圆角背景边框的按钮, 通过调节色彩明度自动计算按下(pressed)状态颜色
2016-11-02 22:13 1861可设置圆角背景边框的的按钮, 通过调节色彩明度自动计算按下(p ... -
网络请求库相关
2016-10-09 09:35 62https://github.com/amitshekhari ... -
ASimpleCache一个简单的缓存框架
2015-10-26 22:53 2123ASimpleCache 是一个为android制定的 轻量级 ... -
使用ViewDragHelper实现的DragLayout开门效果
2015-10-23 10:55 3335先看一下图,有个直观的了解,向下拖动handle就“开门了”: ... -
保证图片长宽比的同时拉伸图片ImageView
2015-10-16 15:40 3679按比例放大图片,不拉伸失真 import android. ...
相关推荐
该源码案例是一款非常不错的增加减少选择控件案例,源码QuantityView,带有增加与减少按钮的数量选择控件。
MFC 带下拉选择列表的按钮控件,包含头文件和CPP文件就可以使用。派生自CButton类。所给的文件是一个MFC工程,不妨先运行试试看看效果!
自带清除按钮的EditText控件,非常好用,以后再也不用一个一个删除输入错误的内容了
带副标题的水晶按钮、包括了控件的写法、GDI+带副标题的水晶按钮、包括了控件的写法、GDI+带副标题的水晶按钮、包括了控件的写法、GDI+带副标题的水晶按钮、包括了控件的写法、GDI+带副标题的水晶按钮、包括了控件的...
C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮...
旋转按钮控件旋转按钮控件旋转按钮控件旋转按钮控件
c#实现圆形按钮控件
VB按钮美化控件,非常好用
C#自定义控件带按钮勾选输入框等常用控件,仿QQ360风格。内有控件源码,可自行修改控件功能。有开发使用demo进行测试使用。
8.如何在程序窗口中创建按钮控件?(Visual C++编程 源代码)8.如何在程序窗口中创建按钮控件?(Visual C++编程 源代码)8.如何在程序窗口中创建按钮控件?(Visual C++编程 源代码)8.如何在程序窗口中创建按钮...
一个可编辑的QTableView控件列表,包括按钮,复选框 日期选择,列表选择等按钮在QTableView中的使用,其中包含不同编译器下的源代码调试,QT编译器和Vs2008编译器两个版本。
这个demo是关于android中自定义控件,在EditText两边放置加减按钮。 需求:带加减按钮的EditText控件。 如有BUG或者不 完善,欢迎小伙伴们提出~
Extjs的日期控件,只能支持到日期选择,对时间的选择并不完善。而网上下载的控件,都是基于Ext.form.dateField 开发,在选中日期后自动选择,并隐藏此...在经过一番改造后,终于做好了一个带确认按钮的时间选择控件。
Duilib列表控件ListEx扩展demo,支持按钮等各种控件;duilib源码下载
JCButton按钮控件正式版JCButton按钮控件正式版JCButton按钮控件正式版JCButton按钮控件正式版
WPF中仅通过XAML代码来实现点击按钮隐藏其他控件,源码
VB漂亮的按钮控件,用户控件, 这款比较经典的JCButton按钮控件,目前已经是最新的V1.7(其实早就出来了:-),目前除了支持多种样式风格的按钮外,还对中文进行完美支持,即UNICODE支持。同时比上一版本额外增加了一个...
WPF没有带时间的日期控件 一直很头疼 这个解决了这个问题 希望对大家有帮助
VB漂亮的按钮控件 VB漂亮的按钮控件 VB漂亮的按钮控件 VB漂亮的按钮控件