博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义View 购物车加减数量
阅读量:7040 次
发布时间:2019-06-28

本文共 4153 字,大约阅读时间需要 13 分钟。

  hot3.png

 首先我们需要的是         加(botton)        减 (botton)      输入数量(editText)    【这里我还设置了最大值】

然后对 加减按钮设置监听,点击对editText的内容进行修改。

 

布局代码:

//android:showDividers="middle"在每个子View间加入分隔线

 

再写我们需要在调用这个自定义View的时候设置它的属性【res  ->   values  ->   attrs.xml】写入

 

最后我们写自定义的LinearLayout,代码如下:

import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.support.annotation.Nullable;import android.text.Editable;import android.text.TextWatcher;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;import cn.jhc.startdemo.R;/** * Created by Administrator on 2017/10/14. */public class AddDeleteCount extends LinearLayout implements View.OnClickListener, TextWatcher {    private int max, btnWidth, edtWidth, addOrDeleteBg;    private EditText edt_count;    //edit控件是否可以修改    private boolean IsEdtCountEdited = false;    public AddDeleteCount(Context context) {        this(context, null);    }    public AddDeleteCount(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        LayoutInflater.from(context).inflate(R.layout.add_or_delete, this);        TextView add = findViewById(R.id.add);        add.setOnClickListener(this);        TextView delete = findViewById(R.id.delete);        delete.setOnClickListener(this);        edt_count = findViewById(R.id.count);        edt_count.addTextChangedListener(this);        if (attrs != null) {            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.AddDeleteCount);            max = array.getInt(R.styleable.AddDeleteCount_maxCount, -1);            btnWidth = array.getDimensionPixelOffset(R.styleable.AddDeleteCount_btnWidth, 100);            edtWidth = array.getDimensionPixelOffset(R.styleable.AddDeleteCount_editWidth, 80);            addOrDeleteBg = array.getColor(R.styleable.AddDeleteCount_AddDeleteBackgroungColor, Color.GRAY);            IsEdtCountEdited = array.getBoolean(R.styleable.AddDeleteCount_isEditCount, false);            array.recycle();        }        edt_count.setEnabled(IsEdtCountEdited);        LayoutParams params = new LayoutParams(btnWidth, ViewGroup.LayoutParams.MATCH_PARENT);        add.setLayoutParams(params);        delete.setLayoutParams(params);        LayoutParams params1 = new LayoutParams(edtWidth, ViewGroup.LayoutParams.MATCH_PARENT);        edt_count.setLayoutParams(params1);        add.setBackgroundColor(addOrDeleteBg);        delete.setBackgroundColor(addOrDeleteBg);    }    @Override    public void onClick(View view) {        int currentCount = Integer.parseInt(edt_count.getText().toString());        switch (view.getId()) {            case R.id.add:                if (currentCount < max) {                    edt_count.setText(String.valueOf(currentCount + 1));                }                break;            case R.id.delete:                if (currentCount > 1) {                    edt_count.setText(String.valueOf(currentCount - 1));                }                break;        }    }    @Override    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {    }    @Override    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {    }    @Override    public void afterTextChanged(Editable editable) {        if (edt_count.getText().toString().isEmpty()) {            return;        } else {            if (Integer.parseInt(edt_count.getText().toString()) > max) {                edt_count.setText(String.valueOf(max));            }        }    }    public void setEdtCountEdited(boolean edtCountEdited) {        edt_count.setEnabled(edtCountEdited);    }    public int getCount() {        if (!edt_count.getText().toString().isEmpty()) {            return Integer.parseInt(edt_count.getText().toString());        }        return 1;    }}

 

 

最后在MainActivity布局中调用:

 

转载于:https://my.oschina.net/huiyun/blog/1550696

你可能感兴趣的文章
小盆友给谷歌写封信 老爸获一周假期
查看>>
Ubuntu安装配置Qt环境
查看>>
LBS 与 GPS 定位之间的区别
查看>>
Android调用系统的Activity、ContentProvider、Service、Broadcast Receiver
查看>>
对象池模式
查看>>
Android学习笔记(四十):Preference的使用
查看>>
ByteArrary(优化数据存储和数据流)
查看>>
围住神经猫,朋友圈瞬间爆红是如何炼成的?
查看>>
HDUoj-------(1128)Self Numbers
查看>>
huffman编码——原理与实现
查看>>
php curl获取网页内容乱码和获取不到内容的解决方法
查看>>
【JavaScript】关于prototype
查看>>
普通Jquery的ajax判断重复和formvalidator的ajaxValidator区别
查看>>
ovs处理openflow消息的流程
查看>>
精品素材:WALK & RIDE 单页网站模板下载
查看>>
大数运算
查看>>
Android开发学习笔记-SharedPreferences的用法
查看>>
JAVA & JSON详解
查看>>
Mac显示隐藏文件的终端命令
查看>>
Spring MVC controller间跳转 重定向 传参 (转)
查看>>