Android按钮控制加减




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

import com.example.customedemo1.view.CustomeComposeWeight;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CustomeComposeWeight customeComposeWeight = (CustomeComposeWeight) findViewById(R.id.test);
        customeComposeWeight.setNumberlistener(new CustomeComposeWeight.NumberLisner() {
            @Override
            public void callBack(int couint) {

                //删除购物车里面的商品

                Toast.makeText(MainActivity.this, "购物车商品数量变化" + couint, Toast.LENGTH_SHORT).show();


            }
        });
    }
}





import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.customedemo1.R;

/**
 * Created by 知足 on 2017/11/29.
 */

public class CustomeComposeWeight extends LinearLayout implements View.OnClickListener {

    private TextView tvDecrease;
    private TextView tvIncrease;
    private EditText etNumber;

    //定一个商品默认数量
    private int count=1;
    //定义一个变量规范这个商品数量的最大值
    private int max=10;
    //定义默认颜色
    private int defaultColor= Color.parseColor("#ce3a2d");
    public CustomeComposeWeight(Context context) {
        this(context,null);
    }

    public CustomeComposeWeight(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomeComposeWeight(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //将自定义属性与当前的这个自定组合控件进行关联
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomeComposeWeight, defStyleAttr, 0);

        //得到集合中的背景颜色属性,如果没有,用默认的颜色
        int color=ta.getColor(R.styleable.CustomeComposeWeight_bgcolor,defStyleAttr);

        View view=View.inflate(context, R.layout.amount_layout,this);
        tvDecrease = view.findViewById(R.id.tvDecrease);
        tvIncrease = view.findViewById(R.id.tvIncrease);
        etNumber = view.findViewById(R.id.etNumber);

        //设置点击事件
        tvDecrease.setOnClickListener(this);
        tvIncrease.setOnClickListener(this);

        //初始两个按钮的颜色
        tvDecrease.setBackgroundColor(color);
        tvIncrease.setBackgroundColor(color);
    }




    public interface NumberLisner{
        void callBack(int couint);
    }

    private  NumberLisner numberLisner;


    public void setNumberlistener(NumberLisner numberLisner){
        this.numberLisner = numberLisner;
    }

    //定义一个方法,给调用者设置最大商品数量
    public  void setGoodsMax(int max){
        this.max=max;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.tvDecrease://减商品的数量
            {
                if(count>0){
                    count--;
                    etNumber.setText(""+count);

                    numberLisner.callBack(Integer.parseInt(etNumber.getText().toString()));
                }
            }
            break;

            case R.id.tvIncrease:
            {
                if(count<max){
                    count++;

                    etNumber.setText(""+count);

                    numberLisner.callBack(Integer.parseInt(etNumber.getText().toString()));
                }
            }
            break;
        }
    }
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

      <TextView
          android:id="@+id/tvDecrease"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:background="@color/colorAccent"
          android:gravity="center"
          android:padding="10dp"
          android:text="减"
          />

    <EditText
        android:id="@+id/etNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:text="1"
        />

    <TextView
        android:id="@+id/tvIncrease"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:layout_weight="1"
        android:padding="10dp"
        android:text="加"
        />

</LinearLayout>



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:xxx="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="com.example.customedemo1.MainActivity">


    <com.example.customedemo1.view.CustomeComposeWeight
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        xxx:bgcolor="@color/colorAccent"
        >

    </com.example.customedemo1.view.CustomeComposeWeight>


</LinearLayout>



attrs
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--声明自己的属性-->
     <declare-styleable name="CustomeComposeWeight">

         <attr name="bgcolor" format="color"/>

     </declare-styleable>
</resources>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值