组合控件 购物车数量加减

一、创建attrs 

<resources>
    <declare-styleable name="AddDerViewAttr" >
        <attr name="left_text" format="string"></attr>
        <attr name="rigth_text" format="string"></attr>
        <attr name="left_text_color" format="color"></attr>
        <attr name="rigth_text_color" format="color"></attr>
        <attr name="edit_text" format="integer"></attr>
     </declare-styleable>

</resources>

二、自定义view

public class AddDerView extends LinearLayout {

    private EditText etnum;
    private TextView der;
    private TextView ad;

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

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

    public AddDerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initview(context,attrs,defStyleAttr);

    }

    //1定义一个接口
    public interface OnAddDerClickListener{
        void onClickAdd(View v);
        void onClickDer(View v);
     }
     //2.声明一个接口对象
     private OnAddDerClickListener listener;
    //3.给外部提供一个设置接口对象的方法
    public void setOnAddDerClickListener(OnAddDerClickListener listener){
        this.listener=listener;

    }

    public int getEtnum() {
        return Integer.valueOf(etnum.getText().toString().trim());
    }

    public void setEtnum(int num) {
         etnum.setText(num+" ");
    }

    private void initview(Context context,AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AddDerViewAttr);
        String left_text = a.getString(R.styleable.AddDerViewAttr_left_text);
        String rigth_text = a.getString(R.styleable.AddDerViewAttr_rigth_text);
        int left_text_color = a.getColor(R.styleable.AddDerViewAttr_left_text_color, Color.BLACK);
        int rigth_text_color = a.getColor(R.styleable.AddDerViewAttr_rigth_text_color, Color.BLACK);
        int edit_text = a.getInteger(R.styleable.AddDerViewAttr_edit_text, 10);



        View view = View.inflate(context, R.layout.addview, this);
        der = view.findViewById(R.id.der);
        ad = view.findViewById(R.id.ad);
        etnum = view.findViewById(R.id.etnum);

        der.setText(left_text);
        ad.setText(rigth_text);
        der.setTextColor(left_text_color);
        ad.setTextColor(rigth_text_color);
        etnum.setText(edit_text+"");

        der.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener!=null){
                    listener.onClickDer(view);
                }
            }
        });

        ad.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener!=null){
                    listener.onClickAdd(view);
                }
            }
        });
    }

    public void setBackground(int color){
        der.setBackgroundColor(color);
        ad.setBackgroundColor(color);
    }

}
三、在ManiActivity中调用接口
public class MainActivity extends AppCompatActivity {

    private AddDerView adv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adv = findViewById(R.id.adv);
        //调用自定义的接口对象
        adv.setOnAddDerClickListener(new AddDerView.OnAddDerClickListener() {
            @Override
            public void onClickAdd(View v) {
                int etnum = adv.getEtnum();
                etnum++;
                adv.setEtnum(etnum);


            }

            @Override
            public void onClickDer(View v) {
                int etnum = adv.getEtnum();
                if (etnum>1){
                    etnum--;
                    adv.setEtnum(etnum);
                }
            }
        });

        adv.setBackground(Color.BLUE);

    }
}
最后在布局中
<com.example.xiangmuday03.AddDerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:left_text="减"
    app:rigth_text="加"
    app:left_text_color="#ffffff"
    app:rigth_text_color="#ffffff"
    app:edit_text="15"
    android:id="@+id/adv"></com.example.xiangmuday03.AddDerView>


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页