首先传两张需要用的图片
add
del
自定义
AddDecreaseView类
/** * Created by 撩个小媳妇 on 2018/5/10. */ public class AddDecreaseView extends LinearLayout{ public AddDecreaseView(Context context) { this(context, null); } public AddDecreaseView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public AddDecreaseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private EditText etNum; //定义接口 public interface OnAddDecreseClickListener { void onClickDecrease(View v); void onClickAdd(View v); } // 2. 声明一个接口对象 private OnAddDecreseClickListener listener; // 3. 给外部提供一个设置接口对象的方法 public void setOnAddDecreaseClickListener(OnAddDecreseClickListener listener) { this.listener = listener; } public int getNum() { return Integer.valueOf(etNum.getText().toString().trim()); } public void setNum(int num) { etNum.setText(num + ""); } private void initView(Context context) { // 注意:第三个参数,一定是this,代表添加视图到本View上 View view = View.inflate(context, R.layout.view_add_decrease, this); TextView txtDecrease = view.findViewById(R.id.btn_del); TextView txtAdd = view.findViewById(R.id.btn_add); etNum = view.findViewById(R.id.et_num); // 6. 在合适的时机触发事件 txtDecrease.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (listener != null) { listener.onClickDecrease(view); } } }); txtAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (listener != null) { listener.onClickAdd(view); } } }); } }
MainActivity.java
public class MainActivity extends AppCompatActivity { private AddDecreaseView adv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adv = findViewById(R.id.adv_show); // 4. 实例化一个接口的对象 AddDecreaseView.OnAddDecreseClickListener listener = new AddDecreaseView.OnAddDecreseClickListener() { @Override public void onClickDecrease(View v) { int num = adv.getNum(); if (num > 1) { num--; adv.setNum(num); } } @Override public void onClickAdd(View v) { int num = adv.getNum(); num++; adv.setNum(num); } }; // 5.实例化好的对象传给内部 adv.setOnAddDecreaseClickListener(listener); } }
view_add_decrease.xml
这里我用的是EditText
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/btn_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/add" /> <EditText android:id="@+id/et_num" android:layout_width="0dp" android:layout_height="70dp" android:layout_weight="8" /> <Button android:id="@+id/btn_del" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/del" /> </LinearLayout>
activity_main.xml
*注意换成自己的包名
<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" android:orientation="horizontal" tools:context="com.example.dianshang_day03.MainActivity"> <com.example.dianshang_day03.AddDecreaseView android:id="@+id/adv_show" android:layout_width="match_parent" android:layout_height="match_parent"></com.example.dianshang_day03.AddDecreaseView> </LinearLayout>