自定义view 之 加减

先看一下效果图:

                


---------------------------------shape-----------------------------

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="200dp"></corners>
    <stroke android:color="@color/colorPrimaryDark" android:width="1dp"></stroke>
</shape>

----------------------------layout_add_delete------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
    android:id="@+id/txt_add"
    android:text="+"
    android:textSize="80dp"
        android:layout_marginRight="30dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/et_number"
        android:text="1"
        android:textSize="80dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/txt_delete"
        android:text="-"
        android:textSize="100dp"
        android:layout_marginLeft="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

---------------------------------------layout_main----------------------------------------

<com.example.view.AddDeleteView
    android:id="@+id/adv_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</com.example.view.AddDeleteView>




------------------------------------------AddDeleteView-----------------------------

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



    //对外提供一个点击的回调接口
    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, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public AddDeleteView(Context context, 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.layout_add_delete,this);
        TextView txtDelete = (TextView) findViewById(R.id.txt_delete);
        TextView txtAdd = (TextView) findViewById(R.id.txt_add);
        etNumber = (TextView) findViewById(R.id.et_number);

        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);
            }
        });

    }
    //对外提供设置number值
    public void setnumber(int number){
        if(number>0&&number<=10){
            etNumber.setText(number+"");
        }
    }
    //获取控件中的值
    public int getnumber(){
        int number=0;
        try {
            //获取控件中的数值
            String trim = etNumber.getText().toString().trim();
            number = Integer.valueOf(trim);//将获取的字符串转换为int类型
        }catch (Exception e){
            number=0;
        }
        return number;
    }
}

----------------------------------------MainActivity--------------------------


public class MainActivity extends AppCompatActivity {
    private AddDeleteView adv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adv = (AddDeleteView) findViewById(R.id.adv_main);
        //调用自定义接口实现接口中的方法
        adv.setOnAddDelClickListener(new AddDeleteView.OnAddDelClickListener() {
            @Override
            public void onAddClick(View v) {
                int origin = adv.getnumber();
                origin++;
                adv.setnumber(origin);
            }

            @Override
            public void onDelClick(View v) {
                int origin = adv.getnumber();
                origin--;
                adv.setnumber(origin);
            }
        });

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值