Android安卓自定义控件实现点击事件等逻辑

要实现Android自定义控件,以便能够进行复用,这里尽行了一些简单的点击事件的设置,还可以进行其他的设置,原理一样

首先需要在layout里面创建一个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:gravity="center_vertical"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle_shape"
        android:gravity="center"
        android:paddingBottom="2dp"
        android:textSize="20sp"
        android:text="--"
        />
    <TextView
        android:id="@+id/num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:gravity="center"
        android:text="1"/>

    <TextView
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle_shape"
        android:gravity="center"
        android:text="+"
        android:textSize="20sp"/>
LinearLayout>
这里的background是自己画的一个circle,代码如下:(这个可以没有)
xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="1dp"
        android:color="#33000000">
    stroke>
    <solid android:color="@android:color/transparent">solid>

shape>

然后自定义一个class继承LinearLayout,


public class AddDelView extends LinearLayout {
    private int count = 1;
    private TextView num;
    private OnItemClick onItemClick;

    public interface OnItemClick {
        public void onItemAddClick(int count);

        public void onItemDelClick(int count);
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

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

    public AddDelView(final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.add_del_view, this);
        TextView add = findViewById(R.id.add);
        TextView del = findViewById(R.id.del);
        num = findViewById(R.id.num);

        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                num.setText(++count + "");
                onItemClick.onItemAddClick(1);
            }
        });

        del.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (count != 1) {
                    count = --count;
                    onItemClick.onItemDelClick(-1);
                }
                num.setText(count >= 1 ? count + "" : 1 + "");

            }
        });
    }

    /**
     * 获取商品数量
     *
     * @return
     */
    public int getCount() {
        return count;
    }

    public void setCount() {
        count = 1;
        num.setText(count + "");

    }
}





你可能感兴趣的:(Android安卓自定义控件实现点击事件等逻辑)