购物车加减器自定义组合控件

一.案例效果

购物车加减器.png

二.技术点描述:

1.自绘控件(完全自定义控件):继承的是RelativeLayout
2.自定义控件,重写三个方法:
(1) onMeasure(int,int)测量:该方法来检查View组件以及它所包含的所有子组件i 的大小。
(2)onLayout(boolean,int,int,int,int)位置:当该组件要分配子组件的位置,大小时,调用。
(3) onDraw(canves) 绘制:当该组件将要绘制它的内容是,回调该方法(使用频率最高,不要在此方法中做耗时操作和对象的建立)但是今天这个案例用不到各位不用担心就是复习一下自定义view的核心方法。

三.代码实现步骤:

1.创建ShopAddView 类


/**
 *@describe(描述):自定义购物车加减器
 *@data(日期): 2019/10/15
 *@time(时间): 13:56
 *@author(作者): fanyanlong
 **/

public class ShopAddView extends RelativeLayout implements View.OnClickListener {
    private EditText mEdit;
    private TextView mTextDelete, mTextAdd;

    public ShopAddView(Context context) {
        super(context);
        init(context);
    }

    public ShopAddView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private Context context;

    private void init(Context context) {
        this.context = context;
        View view = View.inflate(context, R.layout.shop_add, null);
        mEdit = (EditText) view.findViewById(R.id.shop_edit);
        mTextDelete = (TextView) view.findViewById(R.id.shop_delete);
        mTextDelete.setOnClickListener(this);
        mTextAdd = (TextView) view.findViewById(R.id.shop_add);
        mTextAdd.setOnClickListener(this);
        addView(view);
    }

    //传递数量
    public void setCount(int count) {
        mEdit.setText(count + "");
        mEdit.setSelection(mEdit.getText().toString().length());
    }
 //加减器点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shop_delete:
                String count = mEdit.getText().toString().trim();
                int count1 = Integer.parseInt(count);
                if (count1 <= 1) {
                    Toast.makeText(context, "您至少有一个商品", Toast.LENGTH_LONG).show();
                    return;
                }
                count1--;
                mEdit.setText(count1 + "");
                if (mOnNumListener != null) {
                    mOnNumListener.count(count1);
                }
                break;
            case R.id.shop_add:
                String countAdd = mEdit.getText().toString().trim();
                int countAdd1 = Integer.parseInt(countAdd);
                countAdd1++;
                mEdit.setText(countAdd1 + "");
                if (mOnNumListener != null) {
                    mOnNumListener.count(countAdd1);
                }
                break;
        }
    }

    private OnNumListener mOnNumListener;
   
    public void setOnNumListener(OnNumListener mOnNumListener) {
        this.mOnNumListener = mOnNumListener;
    }


    public void setEditCancle() {
        mEdit.setEnabled(false);
        mTextAdd.setEnabled(false);
        mTextDelete.setEnabled(false);
    }
 //定义数量回调接口
    public interface OnNumListener {
        void count(int count);
    }
}

2.创建shop_car_edit.xml文件( EditText 控件描边)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp" />
    <!--描边-->
    <stroke
        android:width="1dp"
        android:color="@color/color_969" />
    <solid android:color="@color/color_4f4" />
</shape>

3. 布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/shop_delete"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="-"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/shop_edit"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_toRightOf="@+id/shop_delete"
            android:background="@drawable/shop_car_edit"
            android:gravity="center_horizontal"
            android:textColor="@color/color_ccc" />

        <TextView
            android:id="@+id/shop_add"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/shop_edit"
            android:gravity="center_horizontal"
            android:text="+"
            android:textSize="14sp" />

    </RelativeLayout>

</RelativeLayout>

4. 调用组合控件Activity /Fragment 中调用自定义ShopAddView 组合控件布局

<?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">
<!--注意这个是包名修改成自己的(com.zd.retrofit_rxjava2.view)-->
  <com.zd.retrofit_rxjava2.view.ShopAddView
        android:id="@+id/addview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </com.zd.retrofit_rxjava2.view.ShopAddView>
</LinearLayout>

5.主页面 MainActivity 实现

         //初始化组合组件
        addView = findViewById(R.id.addview);
        //设置数量
        addView.setCount(2);
        //回调修改后的数量
        addView.setOnNumListener(new ShopAddView.OnNumListener() {
            @Override
            public void count(int count) {
                Log.d(TAG, "count: "+count);
            }
        });

想要源码评论区回复我,给你私发链接

Kotlin基础篇【1】Android Studio 3.0 Kotlin环境配置
Kotlin基础篇【2】初识Kotlin的意义
Kotlin基础篇【3】Kotlin基础语法

=======Kotlin基础篇【4】Kotlin基础语法即将更新=======

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容