加减

属性

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AddDecreaseView" >
    <attr name="left_text" format="string"></attr>
    <attr name="right_text" format="string"></attr>
    <attr name="left_text_color" format="color"></attr>
</declare-styleable>
</resources>
AddDecreaseView
package com.example.day_1;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.w3c.dom.Attr;

/**
 * Created by asus on 2018/4/10.
 */

public class AddDecreaseView extends LinearLayout {
    private EditText etNum;
    private TextView txtDecrease;
    private TextView txtAdd;

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

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

    public AddDecreaseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initview(context, attrs, defStyleAttr);
    }
    // 1. 定义一个接口
    public interface OnAddDecreseClickListener{
        void onClickDecrease(View v);
        void onClickAdd(View v);
    }
    // 2. 声明一个接口对象
    private OnAddDecreseClickListener listener;
    // 3. 给外部提供一个设置接口对象的方法
    public void setOnAddDecreaseClickListener(OnAddDecreseClickListener 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.AddDecreaseView);

        // 规则:AddDecreaseViewAttr_left_text
        // attrs.xml文件中的declare-styleable名字+"_"+attr的名字
        String LeftText = a.getString(R.styleable.AddDecreaseView_left_text);
        String RightText = a.getString(R.styleable.AddDecreaseView_right_text);
        int leftcolor = a.getColor(R.styleable.AddDecreaseView_left_text_color, Color.BLACK);

        a.recycle();

        LeftText= TextUtils.isEmpty(LeftText)? " - " : LeftText;
        RightText = TextUtils.isEmpty(RightText) ? " + " : RightText;
        View view = View.inflate(context, R.layout.item, this);
        //子控件
        txtDecrease = view.findViewById(R.id.txt_decrease);
        txtAdd = view.findViewById(R.id.txt_add);
        etNum = view.findViewById(R.id.et_num);
        // 6. 在合适的时机触发事件
        txtDecrease.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClickDecrease(view);
                }
            }
        });

        txtAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClickAdd(view);
                }
            }
        });
    }
    //加背景色
    public void setBtnBackGround(int color) {
        txtDecrease.setBackgroundColor(color);
        txtAdd.setBackgroundColor(color);
    }
}
MainActivity
package com.example.day_1;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private AddDecreaseView abv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        abv = findViewById(R.id.adv);
        // 4. 实例化一个接口的对象
        AddDecreaseView.OnAddDecreseClickListener listener = new AddDecreaseView.OnAddDecreseClickListener() {
            @Override
            public void onClickDecrease(View v) {
                int num = abv.getEtNum();
                if (num>1){
                    num--;
                    abv.setEtNum(num);
                }
            }

            @Override
            public void onClickAdd(View v) {
                int num = abv.getEtNum();
                num++;
                abv.setEtNum(num);
            }
        };
        abv.setBtnBackGround(Color.RED);
        // 5.实例化好的对象传给内部
        abv.setOnAddDecreaseClickListener(listener);
    }


}
item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/txt_decrease"
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="-"
        android:background="#E2E0E4"
        />
    <EditText
        android:id="@+id/et_num"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="5"/>
    <TextView
        android:id="@+id/txt_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="30dp"
        android:background="#E2E0E4"
        />
</LinearLayout>
activity_main.xml

<com.example.day_1.AddDecreaseView
    android:id="@+id/adv"
    app:left_text="减"
    app:right_text="加"
    app:left_text_color="#00ff00"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></com.example.day_1.AddDecreaseView>

阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

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