组合自定义控件,接口回调实现T

效果图:
在这里插入图片描述
1.自定义布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center">

    <ImageView
        android:id="@+id/sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/goods_sub_btn"/>

    <TextView
        android:id="@+id/textsum"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:text="1"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="#000"/>

    <ImageView
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/goods_add_btn"/>
</LinearLayout>

自定义代码:

public class Group extends FrameLayout implements View.OnClickListener{

    private TextView mTextsum;
    private int value = 1;
    private ImageView mSub;
    private ImageView mAdd;

    public Group(Context context) {
        this(context,null);
    }

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

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

        //初始化
        initView(context);

    }

    private void initView(Context context) {
        //改成this,方便我们初始化对象,??
        View inflate = View.inflate(context, R.layout.item_group, this);
        mSub = inflate.findViewById(R.id.sub);
        mTextsum = inflate.findViewById(R.id.textsum);
        mAdd = inflate.findViewById(R.id.add);

        //获取值
        int textValue = getValue();
        //设置值
        setValue(textValue);

        //设置监听事件
        mSub.setOnClickListener(this);
        mAdd.setOnClickListener(this);

    }

    //设置值
    private void setValue(int textValue) {
        mTextsum.setText(textValue + "");
    }

    //获取值
    private int getValue() {

        String trim = mTextsum.getText().toString().trim();
        //判断非空
        if(!TextUtils.isEmpty(trim)){
            //转成int
            value = Integer.valueOf(trim);
        }
        return value;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //-
           case R.id.sub:
               subNumber();

               break;
               //+
            case R.id.add:

                addNumber();

                break;
        }
    }

    //最大值和最小值
    private int MAX = 5 ;
    private int MIN = 1 ;

    //设置最大值
    public void setMax (int Max){
        MAX = Max ;
    }

    //设置最小值
    public void setMin(int min){
        MIN = min ;
    }

    //+
    private void addNumber() {

        if(value < MAX){
            value ++ ;
        }
        setValue(value);
        //往接口的方法中存储值
        mOnNumberChangeListener.onNumberChange(value);

    }

    //-
    private void subNumber() {
        if(value > MIN){
            value-- ;
        }
        setValue(value);

        mOnNumberChangeListener.onNumberChange(value);
    }



    //定义接口
    public interface OnNumberChangeListener {
        public void onNumberChange(int value);
    }

    //声明接口
    private OnNumberChangeListener mOnNumberChangeListener ;

    //设置接收外界传来的对象方法
    public void setOnNumberChangeListener (OnNumberChangeListener onNumberChangeListener){
        mOnNumberChangeListener = onNumberChangeListener ;
    }

}

MainActivity代码

public class MainActivity extends AppCompatActivity {

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

        //找控件
        Group group = findViewById(R.id.gp);
        //设置最大的值
        group.setMax(20);
        //设置最小
        group.setMin(-2);
        //接口
        group.setOnNumberChangeListener(new Group.OnNumberChangeListener() {
            @Override
            public void onNumberChange(int value) {
                Toast.makeText(MainActivity.this, ""+value, Toast.LENGTH_SHORT).show();
            }
        });


    }
}

猜你喜欢

转载自blog.csdn.net/jiahui6666/article/details/83756616