一、实现思路
整个控件其实是由两个Button和一个EditText组成,直接上代码进行分析。初始化控件,设置了自定义属性(这几个自定义属性的作用大概通过名字也能够知道了)和设置监听器
二.实践
layout文件夹下
1.number_add_sub_view.xml自定义控件的布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:focusable="true"
- android:divider="@drawable/divider"
- android:background="@drawable/bg_amount_layout"
- android:showDividers="middle"
- android:orientation="horizontal"
- >
- <Button
- android:id="@+id/btnDecrease"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:background="@drawable/btn_amount"
- android:text="-"/>
- <EditText
- android:id="@+id/etAmount"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:minWidth="60dp"
- android:layout_weight="2"
- android:background="@null"
- android:inputType="number"
- android:gravity="center"
- android:text="1"/>
- <Button
- android:id="@+id/btnIncrease"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:background="@drawable/btn_amount"
- android:text="+"/>
- </LinearLayout>
2.activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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:focusable="true"
- android:focusableInTouchMode="true"
- tools:context="xxx.addsubdemo.MainActivity">
- //写自己的控件路径!!!
- <com.***.AmountView
- android:id="@+id/amount_view"
- android:layout_width="wrap_content"
- android:layout_height="36dp"
- android:layout_centerInParent="true"
- android:layout_gravity="right"
- android:layout_marginRight="15dp"
- app:btnTextSize="14sp"
- app:btnWidth="36dp"
- app:tvWidth="50dp"/>
- </RelativeLayout>
drawable文件夹下
1.bg_amount_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="#FFFFFF" />
- <stroke
- android:width="1dp"
- android:color="@color/divider" />
- <padding
- android:bottom="1dp"
- android:left="1dp"
- android:right="1dp"
- android:top="1dp" />
- </shape>
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true" android:drawable="@color/divider" />
- <item android:state_enabled="false" android:drawable="@color/divider" />
- <item android:drawable="@android:color/white" />
- </selector>
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle">
- <size
- android:width="0.5dp"/>
- <solid android:color="@color/divider"/>
- </shape>
values文件夹1.自定义属性attrs.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="AmountView">
- <!-- 左右2边+-按钮的宽度 -->
- <attr name="btnWidth" format="dimension" />
- <!-- 中间TextView的宽度 -->
- <attr name="tvWidth" format="dimension" />
- <!--<attr name="tvColor" format="color"/>-->
- <attr name="tvTextSize" format="dimension"/>
- <attr name="btnTextSize" format="dimension"/>
- </declare-styleable>
- </resources>
- <color name="divider">#ffd2d2d2</color>
1.AmountView.class自定义view类:
public class AmountView extends LinearLayout { private static final String TAG = "AmountView"; private TextView etAmount; private Button btnDecrease; private Button btnIncrease; Context context; public AmountView(Context context) { super(context); } public AmountView(Context context, AttributeSet attrs) { super(context, attrs); this.context=context; LayoutInflater.from(context).inflate(R.layout.jiajian, this); etAmount = (TextView) findViewById(R.id.etAmount); btnDecrease = (Button) findViewById(R.id.btnDecrease); btnIncrease = (Button) findViewById(R.id.btnIncrease); TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attrs, R.styleable.AmountView); int btnWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnWidth, LayoutParams.WRAP_CONTENT); int tvWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvWidth, 80); int tvTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvTextSize, 0); int btnTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnTextSize, 0); obtainStyledAttributes.recycle(); LayoutParams btnParams = new LayoutParams(btnWidth, LayoutParams.MATCH_PARENT); btnDecrease.setLayoutParams(btnParams); btnIncrease.setLayoutParams(btnParams); if (btnTextSize != 0) { btnDecrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize); btnIncrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize); } LayoutParams textParams = new LayoutParams(tvWidth, LayoutParams.MATCH_PARENT); etAmount.setLayoutParams(textParams); if (tvTextSize != 0) { etAmount.setTextSize(tvTextSize); } } }2.MainActivity
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- private AmountView mAmountView;
- mAmountView = (AmountView) findViewById(R.id.amount_view);
-
h2.editText=view.findViewById(R.id.etAmount); h2.jian=view.findViewById(R.id.btnDecrease); h2.jia=view.findViewById(R.id.btnIncrease);
h2.editText.setText(data.get(i).getList().get(i1).getNum()+""); //减的点击事件 h2.jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num = data.get(i).getList().get(i1).getNum(); if(num==0){ return; } data.get(i).getList().get(i1).setNum(--num); //刷新接口 p.ShuaCartIPrensenter(0,data.get(i).getList(), (ShowGoodsActivity) context);; } }); //加的点击事件 h2.jia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num = data.get(i).getList().get(i1).getNum(); data.get(i).getList().get(i1).setNum(++num); //刷新接口 p.ShuaCartIPrensenter(0,data.get(i).getList(), (ShowGoodsActivity) context); } });
- }