仿京東商城系列8------自定義的數(shù)量控制器

本項目來自菜鳥窩,有興趣者點(diǎn)擊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ù)量控制器。先看看效果吧。


數(shù)量控制器.gif

內(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.得到加減按鈕和顯示框的實例,進(jìn)行事件添加,和輸入限制。

  • 隨后使用我們的自定義控件的類名,進(jìn)行使用,具體代碼如下:
<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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,288評論 25 708
  • 本項目來自菜鳥窩,有興趣者點(diǎn)擊http://www.cniao5.com/course/ 項目已經(jīng)做完,源碼地址。...
    小莊bb閱讀 2,438評論 0 6
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 46,884評論 22 665
  • 2016年的最后一天,回顧自己這一年走過的路,覺得自己真的經(jīng)歷了好多好多。好像那些事情,是發(fā)生在別人身上。 這一年...
    影子女123左右閱讀 247評論 0 0
  • 這幾天看文章,關(guān)于老好人的內(nèi)容多些。 不懂拒絕,不好意思拒絕,硬著頭皮做下去。 付出了很多,實際上到最后也沒得到什...
    昆侖濯羽閱讀 411評論 4 3