//MainActivity全代码
package com.example.jiajianbuju; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Myview test = findViewById(R.id.test); test.setNumberlistener(new Myview.NumberLisner() { @Override public void callBack(int couint) { Toast.makeText(MainActivity.this, "购物车商品数量变化" + couint,Toast.LENGTH_SHORT).show(); } }); } }
//Myview
package com.example.jiajianbuju; import android.content.Context; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; /** * Created by Administrator on 2018/3/2. */ public class Myview extends LinearLayout implements View.OnClickListener { private Button tvDecrease; private Button tvIncrease; private Button etNumber; private Context context; //定一个商品默认数量 private int count = 1; //定义一个变量规范这个商品数量的最大值 private int max = 1000; //定义默认颜色 // private int defaultColor = Color.parseColor("#ff3660"); public Myview(Context context) { this(context, null); } public Myview(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public Myview(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; //将自定义属性与当前的这个自定组合控件进行关联 //TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Myview, defStyleAttr, 0); //得到集合中的背景颜色属性,如果没有,用默认的颜色 //int color = ta.getColor(R.styleable.Myview_bgcolor, defaultColor); View view = View.inflate(context, R.layout.jiajian, 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 > 1) { count--; etNumber.setText("" + count); numberLisner.callBack(Integer.parseInt(etNumber.getText().toString())); } else { Toast.makeText(context, "已经为1了不能再减了", Toast.LENGTH_SHORT).show(); } } break; case R.id.tvIncrease: { if (count < max) { count++; etNumber.setText("" + count); numberLisner.callBack(Integer.parseInt(etNumber.getText().toString())); } } break; } } }
//activity_main.xml
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.jiajianbuju.MainActivity"> <com.example.jiajianbuju.Myview android:layout_marginTop="300px" android:layout_marginLeft="50px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/test"></com.example.jiajianbuju.Myview> </LinearLayout>
//jiajian.xml
<?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"> <Button android:gravity="center_horizontal" android:id="@+id/tvDecrease" android:layout_width="45px" android:layout_height="45px" android:layout_weight="1" android:background="#ff3660" android:text="-" /> <Button android:gravity="center_horizontal" android:id="@+id/etNumber" android:layout_width="45px" android:layout_height="45px" android:layout_weight="1" android:background="#ffffff" android:text="1" /> <Button android:gravity="center_horizontal" android:id="@+id/tvIncrease" android:layout_width="45px" android:layout_height="45px" android:layout_weight="1" android:background="#ff3660" android:text="+" /> </LinearLayout>
//attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!--声明自己的属性--> <declare-styleable name="Myview"> <attr name="bgcolor" format="color"/> </declare-styleable> </resources>