本項目來自菜鳥窩红淡,有興趣者點擊http://www.cniao5.com/course/
項目已經(jīng)做完不狮,
https://github.com/15829238397/CN5E-shop
仿京東商城系列0------項目簡介
仿京東商城系列1------fragmentTabHost實現(xiàn)底部導(dǎo)航欄
仿京東商城系列2------自定義toolbar
仿京東商城系列3------封裝Okhttp
仿京東商城系列4------輪播廣告條
仿京東商城系列5------商品推薦欄
仿京東商城系列6------下拉刷新上拉加載的商品列表
仿京東商城系列7------商品分類頁面
仿京東商城系列8------自定義的數(shù)量控制器
仿京東商城系列9------購物車數(shù)據(jù)存儲器實現(xiàn)
仿京東商城系列10------添加購物車,管理購物車功能實現(xiàn)
仿京東商城系列11------商品排序功能以及布局切換實現(xiàn)(Tablayout)
仿京東商城系列12------商品詳細(xì)信息展示(nativie與html交互)
仿京東商城系列13------商品分享(shareSDK)
仿京東商城系列14------用戶登錄以及app登錄攔截
仿京東長城系列15------用戶注冊在旱,SMSSDK集成
仿京東商城系列16------支付SDK集成
仿京東商城系列17------支付功能實現(xiàn)
仿京東商城系列18------xml文件讀纫×恪(地址選擇器)
仿京東商城系列19------九宮格訂單展示
仿京東商城系列20------終章
前言
本文我們將介紹一個自定義控件,數(shù)量控制器桶蝎。先看看效果吧驻仅。
內(nèi)容
我們的數(shù)量控制器內(nèi)容相當(dāng)簡單,中間顯示現(xiàn)在的數(shù)值登渣,左右兩側(cè)為加減按鈕噪服。可以限制中間數(shù)值的最大值和最小值胜茧,以及可以設(shè)置兩個按鈕和中間顯示框的背景粘优。接下來我們一起來看如何自定義這樣的一個空間。
- 新建一個xml文件描述空間的內(nèi)部呻顽。代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/subButton"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="60dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:text="-"
android:textColor="@color/black"
/>
<TextView
android:id="@+id/num"
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:gravity="center"
android:text="10" />
<Button
android:id="@+id/addButton"
android:gravity="center"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="60dp"
android:layout_height="35dp"
android:textColor="@color/black"
android:text="+"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_gravity="center"/>
</LinearLayout>
- 新建自定義屬性雹顺,在starrs文件中添加以下自定義屬性
<declare-styleable name="NumControlerView">
<attr name="value" format="reference|integer"/>
<attr name="minValue" format="reference|integer"/>
<attr name="maxValue" format="reference|integer"/>
<attr name="subButtonBackground" format="reference"/>
<attr name="addButtonBackground" format="reference"/>
<attr name="numTextBackGround" format="reference"/>
</declare-styleable>
- 新建自定義控件類,繼承LinearLayout類廊遍。代碼如下:
package com.example.cne_shop.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.cne_shop.R;
/**
* Created by 博 on 2017/7/13.
*/
public class NumControlerView extends LinearLayout implements View.OnClickListener{
private View view ;
private Context context ;
private LayoutInflater inflater ;
private Button addButoon ;
private Button subButton ;
private TextView num ;
private int value ;
private int minValue ;
private int maxValue ;
private Drawable addButtonBackGround ;
private Drawable subButtonBackGround ;
private Drawable numTextBackGround ;
private TypedArray typedArray ;
private onNumChangedListener listener ;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public NumControlerView(Context context) {
this(context , null) ;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public NumControlerView(Context context, @Nullable AttributeSet attrs) {
this(context , attrs ,0) ;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public NumControlerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context ;
inflater = LayoutInflater.from(context) ;
typedArray = context.obtainStyledAttributes(attrs , R.styleable.NumControlerView) ;
initView();
typedArray.recycle();
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void initView(){
view = inflater.inflate(R.layout.num_controller_layout , null , false) ;
addButoon = (Button) view.findViewById(R.id.addButton) ;
subButton = (Button) view.findViewById(R.id.subButton) ;
num = (TextView) view.findViewById(R.id.num) ;
getAttrValue() ;
addButoon.setBackground(addButtonBackGround);
subButton.setBackground(subButtonBackGround);
num.setBackground(numTextBackGround);
num.setText(value+"");
addButoon.setOnClickListener(this);
subButton.setOnClickListener(this);
num.setOnClickListener(this);
addView(view);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
num.setText(value+"");
}
public int getMinValue() {
return minValue;
}
public void setMinValue(int minValue) {
this.minValue = minValue;
}
public int getMaxValue() {
return maxValue;
}
public void setMaxValue(int maxValue) {
this.maxValue = maxValue;
}
public void getAttrValue(){
maxValue = typedArray.getInt(R.styleable.NumControlerView_maxValue , 0) ;
value = typedArray.getInt(R.styleable.NumControlerView_value , 0) ;
minValue = typedArray.getInt(R.styleable.NumControlerView_minValue , 0) ;
addButtonBackGround = typedArray.getDrawable(R.styleable.NumControlerView_addButtonBackground ) ;
subButtonBackGround = typedArray.getDrawable(R.styleable.NumControlerView_subButtonBackground ) ;
numTextBackGround = typedArray.getDrawable(R.styleable.NumControlerView_numTextBackGround ) ;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.addButton){
if (addValue() && listener != null)
listener.addValueListener(v , getValue());
}else if (v.getId() == R.id.subButton){
if (subValue() && listener != null)
listener.subValueListener(v , getValue());
}
}
private Boolean addValue(){
if( num.getText()!=null && value != maxValue ) {
value++ ;
num.setText(value+"");
return true ;
}else {
Toast.makeText( context ,"已達(dá)到允許的最大值" , Toast.LENGTH_SHORT ).show();
return false ;
}
}
private Boolean subValue(){
if( num.getText()!=null && value != minValue ) {
value-- ;
num.setText(value+"");
return true ;
}else {
Toast.makeText( context ,"已達(dá)到允許的最小值" , Toast.LENGTH_SHORT ).show();
return false ;
}
}
public interface onNumChangedListener{
void addValueListener(View v, int value) ;
void subValueListener(View v, int value) ;
}
public void setValueChangeListener(onNumChangedListener listener){
this.listener = listener ;
}
}
上述代碼完成以下工作:
1.實現(xiàn)構(gòu)造方法嬉愧。
2.調(diào)用context.obtainStyledAttributes(attrs , R.styleable.NumControlerView) 方法得到typedArray 。用來取出對應(yīng)的自定義屬性的值喉前。
3.得到加減按鈕和顯示框的實例没酣,進行事件添加王财,和輸入限制。
- 隨后使用我們的自定義控件的類名四康,進行使用搪搏,具體代碼如下:
<com.example.cne_shop.widget.NumControlerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:value="1"
app:minValue="1"
app:maxValue="10"
app:numTextBackGround="@color/white"
app:addButtonBackground="@color/white"
app:subButtonBackground="@color/white"
></com.example.cne_shop.widget.NumControlerView>