美文网首页面试题
购物车加减器自定义组合控件

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

作者: 职度 | 来源:发表于2019-10-15 18:36 被阅读0次

一.案例效果

购物车加减器.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基础语法即将更新=======

相关文章

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

    一.案例效果 二.技术点描述: 1.自绘控件(完全自定义控件):继承的是RelativeLayout2.自定义控件...

  • Android入门06 -- 自定义控件

    自定义组合控件 将几个子控件组合在一起,形成一个可复用的新的组合控件,自定义组合控件一般继承自RelativeLa...

  • 【Android】自定义控件

    Android自定义控件可以有三种实现方式 组合原生控件自己绘制控件继承原生控件 1 组合原生控件 1.1 组合原...

  • android2019-01-03

    1.View的绘制流程自定义控件:1、组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成的新控件。...

  • 11-自定义组合控件以及使用

    一、自定义组合控件介绍 开发中,为了使用的方便,经常把一些控件组合成一个控件,那样就成为了我们的自定义组合控件,严...

  • Android组合控件详解 & 自定义属性

    组合控件详解 & 自定义属性 摘录来源:极客学院WiKi 组合控件是自定义控件的一种,只不过它是由其他几个原生控件...

  • 组合控件2——海贼王选项菜单

    之前的自定义控件——初识自定义控件,我们了解到了自定义控件分为三种,自制控件,组合控件,拓展控件。而我们在自制控件...

  • 自制控件3——仿qq侧滑删除

    在自定义控件——初识自定义控件里面,我们已经对自定义控件进行描述和分类。其分类分别是 自制控件 组合控件 拓展控件...

  • 自制控件2 —— 自制控件 仿qq侧滑菜单

    在自定义控件——初识自定义控件里面,我们已经对自定义控件进行描述和分类。其分类分别是 自制控件 组合控件 拓展控件...

  • 每天五道Android面试题,轻松进大厂2018-12-20

    一、View的绘制流程 自定义控件: 1、组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成的新控...

网友评论

    本文标题:购物车加减器自定义组合控件

    本文链接:https://www.haomeiwen.com/subject/ttzsmctx.html