android 购物车数量加减,自定义View 购物车加减数量
首先我们需要的是 加(botton) 减 (botton) 输入数量(editText) 【这里我还设置了最大值】
然后
对 加减按钮设置监听,点击对editText的内容进行修改。
布局代码:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable="true"
android:showDividers="middle">
//android:showDividers="middle"在每个子View间加入分隔线
android:id="@+id/add"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@color/gray"
android:gravity="center"
android:minWidth="30dp"
android:text="+"
android:textColor="@color/write" />
android:id="@+id/count"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@null"
android:gravity="center"
android:inputType="number"
android:minWidth="40dp"
android:text="1"
android:textColor="@color/gray" />
android:id="@+id/delete"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@color/gray"
android:gravity="center"
android:minWidth="30dp"
android:text="-"
android:textColor="@color/write" />
再写我们需要在调用这个自定义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布局中调用:
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"
tools:context="cn.jhc.startdemo.duty.DutyActivity"
android:orientation="vertical">
android:id="@+id/add_delete"
android:layout_width="300dp"
android:layout_height="40dp"
app:maxCount="5"
app:btnWidth="100dp"
app:editWidth="100dp"
app:isEditCount="false"
app:AddDeleteBackgroungColor="@color/gray"
app:textSize="14dp"/>
android 购物车数量加减,自定义View 购物车加减数量相关推荐
- Carson带你学Android:源码解析自定义View Draw过程
前言 自定义View是Android开发者必须了解的基础 网上有大量关于自定义View原理的文章,但存在一些问题:内容不全.思路不清晰.无源码分析.简单问题复杂化 等 今天,我将全面总结自定义View ...
- Android中实现Bitmap在自定义View中的放大与拖动
一基本实现思路: 基于View类实现自定义View –MyImageView类.在使用View的Activity类中完成OnTouchListener接口,实现对MotionEvent事件的监听与处理 ...
- Android软件开发之盘点自定义View界面大合集(二)
Android软件开发之盘点自定义View界面大合集(二) - 雨松MOMO的程序世界 - 51CTO技术博客 雨松MOMO带大家盘点Android 中的自定义View界面的绘制 今天我用自己写的一个 ...
- Android 雪花飘落动画效果 自定义View
在码农的世界里,优美的应用体验,来源于程序员对细节的处理以及自我要求的境界,年轻人也是忙忙碌碌的码农中一员,每天.每周,都会留下一些脚印,就是这些创作的内容,有一种执着,就是不知为什么,如果你迷茫,不 ...
- android 仿360浮动,Android仿360悬浮小球自定义view实现示例
Android仿360悬浮小球自定义view实现示例 效果图如下: 实现当前这种类似的效果 和360小球 悬浮桌面差不错类似.这种效果是如何实现的呢.废话不多说 ,直接上代码. 1.新建工程,添加悬浮 ...
- android圆形点击效果,Android 三种方式实现自定义圆形页面加载中效果的进度条
[实例简介] Android 三种方式实现自定义圆形页面加载中效果的进度条 [实例截图] [核心代码] ad376a86-a9aa-49bc-8cea-321bcff2c0c3 └── AnimRou ...
- Android仿IOS滑动关机-自定义view系列(6)
Android仿IOS滑动关机-自定义view系列 功能简介 GIf演示 主要实现步骤-具体内容看github项目里的代码 Android技术生活交流 更多其他页面-自定义View-实用功能合集:点击 ...
- android仿微博头像_Android 自定义 View 集锦|自定义圆形旋转进度条,仿微博头像加载效果...
微博 App 的用户头像有一个圆形旋转进度条的加载效果,看上去效果非常不错,如图所示: 据说 Instagram 也采用了这种效果.最近抽空研究了一下,最后实现的效果是这样: 基本上能模拟出个大概,代 ...
- android覆盖扩散动画,[Android]多层波纹扩散动画——自定义View绘制
之前整理过一些属性动画的基本操作,这一段时间的动画相关需求都安然度过了.直到这次-- 一.另一种动画需求 多数交互中的动画都是让单个页面元素动起来,这种就很适合用属性动画实现.但是对于 多个元素.非页 ...
最新文章
- Lossless Codec---APE代码解读系列(二)
- 加速企业数字化转型,首届Spring Summit技术峰会圆满落幕
- 利用Nginx轻松实现Ajax的跨域请求(前后端分离开发调试必备神技)
- js获取上传文件内容
- C#操作Word完全功略!
- 简单约瑟夫环问题解法汇总(模拟/数论)
- 我去德国出差后学习的一些德语
- linux的系统移植——【PC-开发板】的环境搭建
- Oracle标准审计实战过程详解
- Android新手之旅(12) URL解码
- 基于HT for Web的3D树的实现
- CentOs安装pyhive
- 什么是NP完全问题?
- python做估值模型_理解债券估值中的摊销/摊余成本,利用EXCEL+Python估值建模
- 光耦隔离的作用是什么?
- RabbitMQ 四种类型发送接收数据方式
- C语言源代码系列-管理系统之单项选择题标准化考试系统设计
- 【Druid】Druid连接池(三)——配置监控界面设置登录界面
- Linux与Android安全差异
- 超级机器人大战A(GBA)帅气攻略(超级系流程2)
热门文章
- Python之reverse函数
- 图片浏览器开发日志-14(ComboBox和ListBox toolips实现)
- C语言字符串函数,内存函数讲解及其模拟
- Microsoftnbsp;Projectnbsp;2010…
- 学习笔记——exec族函数详解(execl, execlp, execle, execv, execvp, execvpe )
- Windows生成公钥和私钥
- HTML页面中的表格导出为EXCEL文件
- 行测-言语类题目总结
- python设置散点图点的大小_python散点图区域大小比例轴长度
- 所谓风水 其实说的是WiFi