组合控件-加减器+最大值变更

主页面

package com.example.zy_zuhekongjian_jiajianqi;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zy_zuhekongjian_jiajianqi.R;

public class MainActivity extends AppCompatActivity {
    private MyView mv;
    private TextView tv_value;
    private int a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mv = (MyView) findViewById(R.id.jia);
        //可以直接改变最大值
        a = mv.setMaxvalue(5);
        //获取id
        tv_value = findViewById(R.id.tv_value);
        //获取值
        String value = tv_value.getText().toString().trim();
        mv.setOnNumberChangerListener(new MyView.OnNumberChangerListener(){
            @Override
            public void OnNumberChanger(int value) {
                //属于我自己的业务逻辑
                //Toast.makeText(MainActivity.this, "变化的数量值"+value, Toast.LENGTH_SHORT).show();
                if (value == 0){
                    Toast.makeText(MainActivity.this, "已经为0不能再减", Toast.LENGTH_SHORT).show();
                }else if(value == a){
                    Toast.makeText(MainActivity.this, "商品只有"+a+"件", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

自定义类

package com.example.zy_zuhekongjian_jiajianqi;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyView extends LinearLayout implements View.OnClickListener {

    private ImageButton img_sub;
    private ImageButton img_add;
    private TextView tv_value;
    private int value;
    private int valuemax;


    //1.创建对象的时候
    public MyView(Context context) {
        this(context, null);
    }

    //2.XML中使用的时候回调
    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs ,  0);
    }

    //3.在XML中使用,且使用Style风格中.
    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        //把一个布局文件实例化,并且加载到AddSuVIew类中
        View inflate = View.inflate(context, R.layout.item, this);
        //初始化控件
        img_sub = (ImageButton) inflate.findViewById(R.id.iv_sub);
        img_add = (ImageButton) inflate.findViewById(R.id.iv_add);
        tv_value = (TextView) inflate.findViewById(R.id.tv_value);

        img_add.setOnClickListener(this);
        img_sub.setOnClickListener(this);

        //获取Value值
        value = getValue();
        //设置valus的值
        setValue(value);

    }
    /**
     * 这里获取Value是从UI那里拿到值
     * @return
     */
    public int getValue() {
        //获取textview中的值
        String trim = tv_value.getText().toString().trim();
        //判断是否有值
        if (!TextUtils.isEmpty(trim)){
            //获取出来,因为其值是字符串,所以要进行Int型转换
            value =Integer.valueOf(trim);
        }
        return value;
    }

    //赋值的方法
    public void setValue(int value) {
        tv_value.setText(value+"");
    }
    //最大值变更的方法
    public int setMaxvalue(int maxvalue) {
        valuemax = maxvalue;
        return maxvalue;
    }

    //ImageView按钮的点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //添加
            case R.id.iv_add:
                addNumber();
                break;
            //减少
            case R.id.iv_sub:
                subNumber();
                break;
            default:
                break;
        }
    }
    //添加的方法
    private void addNumber() {
        if (value < valuemax)
        {
            value++;
        }
        setValue(value);

        if (mOnNumberChangerListener !=null){
            mOnNumberChangerListener.OnNumberChanger(value);
        }
    }
    //减少的方法
    private void subNumber() {
        if (value > 0){
            value--;
        }
        setValue(value);
        //当按钮是Value值发生变化时,回调该接口方法
        if (mOnNumberChangerListener !=null){
            mOnNumberChangerListener.OnNumberChanger(value);
        }
    }

    /**
     * B.定义接口,及所要调用的接口方法,当商品数量发生变化时,回调给接口
     */
    public interface OnNumberChangerListener{
        void OnNumberChanger(int value);
    }

    //定义接口对象
    private OnNumberChangerListener mOnNumberChangerListener;

    //设置方法接收外界传来的接口对象方法
    public void setOnNumberChangerListener(OnNumberChangerListener onNumberChangerListener){
        mOnNumberChangerListener =onNumberChangerListener;
    }

}

主页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zy_zuhekongjian_jiajianqi.MainActivity">

    <com.example.zy_zuhekongjian_jiajianqi.MyView
        android:id="@+id/jia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
自定义布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageButton
        android:id="@+id/iv_sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/goods_sub_btn"/>
    <TextView

        android:id="@+id/tv_value"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_gravity="center"
        android:text="1"/>
    <ImageButton
        android:id="@+id/iv_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/goods_add_btn"/>

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值