android_购物车上的自定义加减器

//自定义加减器的view

public class AddDeleteView extends LinearLayout {
    private OnAddDelClickListener listener;
    private EditText etNumber;

    //对外提供一个点击的回调接口
    public interface OnAddDelClickListener {
        void onAddClick(View v);

        void onDelClick(View v);
    }

    public void setOnAddDelClickListener(OnAddDelClickListener listener) {
        if (listener != null) {
            this.listener = listener;
        }
    }

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

    public AddDeleteView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AddDeleteView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        View.inflate(context, R.layout.adddelete, this);
        TextView txtDelete = (TextView) findViewById(R.id.tv_delete);
        TextView txtAdd = (TextView) findViewById(R.id.tv_add);
        etNumber = (EditText) findViewById(R.id.ed_num);


        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AddDeleteView);

        String leftText = typedArray.getString(R.styleable.AddDeleteView_left_item);
        String rightText = typedArray.getString(R.styleable.AddDeleteView_right_item);
        String middleText = typedArray.getString(R.styleable.AddDeleteView_middle_item);

        txtDelete.setText(leftText);
        txtAdd.setText(rightText);
        etNumber.setText(middleText);

        //回收
        typedArray.recycle();


        txtDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onDelClick(view);
            }
        });

        txtAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onAddClick(view);
            }
        });

    }

    //对外提供一个修改数字的方法
    public void setNumber(int number) {
        if (number > 0) {
            etNumber.setText(number + "");
        }
    }

    //对外提供一个获取当前数字的方法
    public int getNumber() {
        String string = etNumber.getText().toString();
        int i = Integer.parseInt(string);
        return i;
    }
}

//自定义加减器的布局

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

    <TextView
        android:id="@+id/tv_delete"
        android:layout_width="31dp"
        android:layout_height="31dp"
        android:background="#999999"
        android:gravity="center"
        android:text="-" />

    <EditText
        android:id="@+id/ed_num"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@null"
        android:gravity="center" />

    <TextView
        android:id="@+id/tv_add"
        android:layout_width="31dp"
        android:layout_height="31dp"
        android:background="#999999"
        android:gravity="center"
        android:text="+" />
LinearLayout>

//自定义的资源文件

<resources>
    <declare-styleable name="AddDeleteView">
        <attr name="left_item" format="string">attr>
        <attr name="right_item" format="string">attr>
        <attr name="middle_item" format="string">attr>
    declare-styleable>
resources>

你可能感兴趣的:(购物车-android)