自定义VIew实现加减器(eg:京东)

本文介绍了一个自定义Android视图组件MyView的实现过程,包括如何继承FrameLayout并重写构造方法,如何通过inflate引入自定义布局,以及如何设置TextView的属性如字体大小、颜色等,并为按钮添加点击事件来改变EditText中的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class MyView extends FrameLayout {  //继承FrameLayout   重写VIew的四个构造方法
    private Button subtract;
    private Button insert;
    private EditText num;

    public MyView(Context context) {
        this(context, null);//将 super改为this   是为了构造方法之间能够有联系。
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        //}

//   public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
        //通过inflate将布局引入并将view加入Fragment容器中
        View view = View.inflate(context, R.layout.jiajian_layout, this);//引入自定义布局
        initView();

 //设置属性  //通过obtainStyledAttributes设置参数属性  返回设置属性参数的一个数组
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
             //设置字体大小
        int textsize = typedArray.getDimensionPixelSize(R.styleable.MyView_LeftTextSize, 0);
            //设置字体颜色
        int color = typedArray.getColor(R.styleable.MyView_LeftTextColor, 0);
        //设置背景图片
        Drawable background = typedArray.getDrawable(R.styleable.MyView_LeftBackground);
        typedArray.recycle();
        //判断字体大小  颜色>0时才有效果
        if(textsize>0){
            //左边的按钮设置属性
          //  subtract.setTextSize(textsize);
            num.setTextSize(textsize);
        }
        if(color!=0){
          //  subtract.setTextColor(color);
            num.setTextColor(color);
        }
        if(background!=null){
            subtract.setBackground(background);
        }


        //给加减按钮添加点击事件
        subtract.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取到输入框的值(int 格式  再进行--)
                int numm = getNumberValue();
                numm--;
                setNumView(numm);
                Log.i("cc", "====" + numm);
            }
        });
        //给加减按钮添加点击事件
        insert.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取到输入框的值(int 格式  再进行++)
                int numm = getNumberValue();
                numm++;
                Log.i("cc", "====" + numm);
                setNumView(numm);
            }
        });


    }

    //写一个获取数量的方法
    private int getNumberValue() {
        //初始化一个变量
        int numm = 0;
        String number = num.getText().toString();
        //判断输入框的值是否为空‘
        if (TextUtils.isEmpty(number)) {//如果不为空
            //让输入框的值返回==1
            numm = 1;
        }
        //返回输入框的数值  转换成int格式
        return Integer.parseInt(number);
    }

    //判断最小值
    private void setNumView(int numm){
            if(numm<=0){
             numm=1;
                subtract.setEnabled(false);//等于0时   按钮失去效果
            }else{
                subtract.setEnabled(true);//有效果

            }
        num.setText(Integer.toString(numm));
    }


    private void initView() {
        subtract = (Button) findViewById(R.id.subtractid);
        insert = (Button) findViewById(R.id.insertid);
        num = (EditText) findViewById(R.id.numid);
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值