加减器

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.example.e.combawaychenqing0710.AddDecView
        android:id="@+id/adv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:addText="+"
        app:max="20"
        app:textBackGround="#0080C0" />
</LinearLayout>

--------------------------------------------------- 
public class MainActivity extends AppCompatActivity {
    private AddDecView adv;
    private Button btnGet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adv = (AddDecView) findViewById(R.id.adv);

//        adv.setCount(10);
        adv.setListener(new AddDecView.onClickLisner() {
            @Override
            public void onClick(int count) {
                Toast.makeText(MainActivity.this,"自定义view的当前值是",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
---------------------------------------------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txt_view_decresease"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:background="#868686"
        android:padding="10dp"
        android:text="-" />

    <TextView
        android:id="@+id/txt_view_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:background="#868686"
        android:padding="10dp"
        android:text="+" />

    <EditText
        android:id="@+id/et_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_toLeftOf="@id/txt_view_add"
        android:layout_toRightOf="@id/txt_view_decresease"
        android:background="@null"
        android:gravity="center"
        android:text="0" />

</RelativeLayout>
-------------------------------------------------------------------
public class AddDecView extends LinearLayout implements View.OnClickListener {
    private TextView txtDecrease;
    private TextView txtAdd;
    private EditText etShow;
    private int count = 0;
    private int defaultColor = Color.parseColor("#0080C0");
    private int max = 0;

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

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

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

        // 通过这条语句把我们自定义的属性引入进来
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyleAttr, 0);
        // 自定义属性的id格式  stylable的名字_attr属性名字,可以有下面几种类型
        // string,color,demension,integer,enum,reference,float,boolean,fraction,flag;
        int bgColor = a.getColor(R.styleable.CustomTitleView_textBackGround, defaultColor);
        max = a.getInt(R.styleable.CustomTitleView_max, 0);
        String addText = a.getString(R.styleable.CustomTitleView_addText);
        String decText = a.getString(R.styleable.CustomTitleView_decText);


        //第三个参数是this,代表给当前view设置视图
        View view = View.inflate(context, R.layout.view_add_decrease, this);
//        View view = View.inflate(context, R.layout.view_add_decrease, null);
        txtDecrease = (TextView) view.findViewById(R.id.txt_view_decresease);
        txtAdd = (TextView) view.findViewById(R.id.txt_view_add);
        etShow = (EditText) view.findViewById(R.id.et_show);

        txtDecrease.setText(TextUtils.isEmpty(decText)?"-":decText);
        txtAdd.setText(TextUtils.isEmpty(addText)?"+":addText);


        etShow.setText(R.string.app_name);
//        etShow.setText(count);

        txtAdd.setOnClickListener(this);
        txtDecrease.setOnClickListener(this);

        txtDecrease.setBackgroundColor(bgColor);
        txtAdd.setBackgroundColor(bgColor);
        // 调用TypedArray的回收方法
        a.recycle();
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
        etShow.setText(count + "");
    }

    interface onClickLisner {
        void onClick(int count);
    }

    private onClickLisner listener;
    public void setListener(onClickLisner listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txt_view_add:
                if (count < max) {
                    count++;
                    etShow.setText(count + "");
                }
                break;
            case R.id.txt_view_decresease:
                if (count > 0) {
                    count--;
                }
                etShow.setText(count + "");
                listener.onClick(count);
                break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值