(安卓)自定义控件,自定义(view)中的自定义属性


先来看看主xml中的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bwie.com.day_custom_control_1213.MainActivity">

    <bwie.com.day_custom_control_1213.AddDelView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/ade"
        app:left_text="加"
        app:right_text="减"
        app:middle_text="5"
        app:left_text_color="#03fbde"

        ></bwie.com.day_custom_control_1213.AddDelView>


</RelativeLayout>




在上面的 布局中可以看到:   整体是一个自定义控件


                                        这些是自定义属性


首先自定义属性需要在values文件下写一个attrs.xml:


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="AddDelViewStyle">
        <attr name="left_text" format="string"/>
        <attr name="right_text" format="string"/>
        <attr name="middle_text" format="string"/>
        <attr name="left_text_color" format="color"/>
    </declare-styleable>

</resources>

这是自定义属性所需要的



接下来看看自定义控件的布局:

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

    <TextView
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/add"
        android:text="+"
        android:background="#aea9a9"
        />
    <EditText
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/et"
        android:text="1"
        android:gravity="center"
        />

    <TextView
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/del"
        android:text="-"
        android:background="#aea9a9"
        />



</LinearLayout>

=======================================================


上代码:

首先看看主MainActivity中的代码:

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

public class MainActivity extends AppCompatActivity {

    private AddDelView ade;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //在主xml中引入自己写的自定义布局的文件

        //自定义的布局控件
        ade = (AddDelView)findViewById(R.id.ade);

        //自定义控件的加减以及EditText 。这三个的回调接口
        ade.setOnAddDelClickLinstener(new AddDelView.OnAddDelClickLinstener() {
            @Override
            public void onAddClick(View v) {
                int number = ade.getNumber();
                number++;
                ade.setNumber(number);
            }

            @Override
            public void onDelClick(View v) {
                int number = ade.getNumber();
                number--;
                ade.setNumber(number);
            }
        });
    }
}

接下来自定义控件中的代码  

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

/**
 * Created by 迷人的脚毛!! on 2017/12/13.
 */

public class AddDelView extends LinearLayout {


    private EditText et;

    //定义接口
    interface OnAddDelClickLinstener{
        void onAddClick(View v);
        void onDelClick(View v);

    }
    //定义接口方法
    private OnAddDelClickLinstener linsanter;
    //定义接口方法
    public void setOnAddDelClickLinstener(OnAddDelClickLinstener linsanter){
        this.linsanter=linsanter;
    }

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

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

    public AddDelView(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 view = View.inflate(context, R.layout.layout_del_add, this);

        TextView add=(TextView) findViewById(R.id.add);
        TextView del=(TextView) findViewById(R.id.del);
        et = (EditText)findViewById(R.id.et);
        //自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AddDelViewStyle);
        String leftText = typedArray.getString(R.styleable.AddDelViewStyle_left_text);
        String rightText = typedArray.getString(R.styleable.AddDelViewStyle_right_text);
        String middleText = typedArray.getString(R.styleable.AddDelViewStyle_middle_text);
        int color = typedArray.getColor(R.styleable.AddDelViewStyle_left_text_color, Color.RED);

        //自定义属性设置
        add.setText(leftText);
        add.setTextColor(color);
        del.setText(rightText);
        et.setText(middleText);
        //因为是数组,所以需要回收
        typedArray.recycle();



        //设置接口点击事件
        //加
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                linsanter.onAddClick(view);
            }
        });
        //减
        del.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                linsanter.onDelClick(view);
            }
        });
    }
    //定义赋值的方法
    public void setNumber(int num){
        if (num>0){
            et.setText(num+"");
        }

    }
    //定义取值的方法
    public int getNumber(){
        int aa=0;
        try {
            String trim = et.getText().toString().trim();
            aa = Integer.valueOf(trim + "");
        }catch (Exception o){
            aa=0;
        }
        return aa;
    }


}


 

以上就是最基础的自定义控件以及自定义属性。。。。。

   





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值