android代码中加减,Android 加减

本文主要介绍了Android中自定义加减组合控件的实现。包含布局文件add_and_subtract.xml的设置,以及自定义类AddAndSubtractView的代码实现,其中有加减监听、加减方法、更新数量等功能,还提供了点击接口。

add_and_subtract.xml(布局文件)

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:background="#a9a7a7">

android:id="@+id/tvSub"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="10dp"

android:text="--"

android:textColor="@color/colorAccent"

android:textSize="14sp" />

android:id="@+id/tvNumber"

android:layout_width="80dp"

android:layout_height="wrap_content"

android:gravity="center"

android:padding="10dp"

android:textColor="@color/colorAccent"

android:textSize="14sp" />

android:id="@+id/tvAdd"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="10dp"

android:text="+"

android:textColor="@color/colorAccent"

android:textSize="14sp" />

AddAndSubtractView 加减 (自定义组合控件)

public class AddAndSubtractView extends LinearLayout {

private View mView;

private TextView add,sub,number;

private OnNumChangedListener onNumChangedListener;

public AddAndSubtractView(Context context) {

this(context,null);

}

public AddAndSubtractView(Context context,AttributeSet attrs) {

this(context, attrs,-1);

}

public AddAndSubtractView(Context context,AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

//添加布局和初始化控件

initView(context);

//监听

initListenter();

}

//加减的监听

private void initListenter() {

//加

add.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//调用加数的方法

add();

}

});

//减

sub.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//调用减数的方法

sub();

}

});

}

//减得方法

private void sub() {

//控件初始化的时候已经给了 1 获取

String sub = number.getText().toString();

//String类型转换成Int类型

int parseInt = Integer.parseInt(sub);

//判断

if (parseInt>1){

parseInt--;

setCurentCount(parseInt);

}else {

ToastUtil.showToast("不能再少了");

}

}

//加的方法

private void add() {

//控件初始化的时候已经给了 1 获取

String add = number.getText().toString();\

//String类型转换成Int类型

int parseInt = Integer.parseInt(add);

parseInt++;

setCurentCount(parseInt);

}

//加减的方法都调用它 把加减的值传给接口

public void setCurentCount(int parseInt) {

//更新 数量

number.setText(parseInt+"");

//判断

if (onNumChangedListener!=null){

onNumChangedListener.onNumChanged(this,parseInt);

}

}

public int getCurentCount(){

return Integer.parseInt(number.getText().toString());

}

//set方法

public void setOnNumChangedListener(OnNumChangedListener onNumChangedListener) {

this.onNumChangedListener = onNumChangedListener;

}

//初始化控件

private void initView(Context context) {

//添加组合控件的布局文件

mView=View.inflate(context,R.layout.add_and_subtract,this);

//控件初始化

add=mView.findViewById(R.id.tvAdd);

sub=mView.findViewById(R.id.tvSub);

number=mView.findViewById(R.id.tvNumber);

//初始的时候给1

number.setText("1");

}

//提供点击的接口

public interface OnNumChangedListener {

void onNumChanged(View view,int curNum);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值