android-自定义组合控件(EditText+选项)

一.前言
在开发中,或许一个业务需求中会出现很多系统控件组合成的布局,并且经常需要复用。比如在一个表单中,里面有个编辑框EditText右侧还需要有个选项卡按钮,需要有编辑框的输入功能也需要有右侧选项卡的点击事件,同时这两个控件也存在一定关联,且在一个界面出现很多次,这个时候可以设计一个属于自己的组合View控件.
二.开发流程
1.新建一个attrs.xml属性集文件,即你要自定义控件的属性
2.新建一个xml布局文件,要显示的组合控件的布局
3.新建一个类,继承FrameLayout,LinearLayout或者RelativeLayout等
4.得到属性集对象TypeArray
5.通过属性集得到各个属性及设置属性
6.向外暴露set设置属性的方法
7.向外暴露事件接口供调用
三.示例
1.功能:一个编辑框,右侧有个选项按钮,点击选项按钮弹出Popwindow,里面是个Recylerview列表,点击列表后把对应的值赋值到编辑框里面.
2.代码

package cn.qzzg.mobilenurse.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import cn.qzzg.mobilenurse.R;
import cn.qzzg.mobilenurse.adapter.recyclerview.BaseRVAdapter;
import cn.qzzg.mobilenurse.fragment.access.enterHos.adapter.PopListAdapter;
import cn.qzzg.mobilenurse.popwindow.CustomPopWindow;
import cn.qzzg.mobilenurse.utils.ToastUtils;

/**
 * Created by lss0555 on 2018/10/12/012.
 * description:编辑框结合下拉布局
 */

public class SelectEditText  extends RelativeLayout{

    private float editText_width;
    private EditText mEtName;
    private RelativeLayout mRtlSelect;
    private Context context;
    private CustomPopWindow popWindow;

    public SelectEditText(@NonNull Context context) {
        super(context);
    }

    public SelectEditText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initview(context,attrs);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    public void initview(Context context,@Nullable AttributeSet attrs){
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.item_edittext_select,this,true);
        //自定义属性
        TypedArray attributes=context.obtainStyledAttributes(attrs,R.styleable.SelectEditText);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mEtName = (EditText) findViewById(R.id.et_name);
        mRtlSelect = (RelativeLayout)findViewById(R.id.rtl_select);

        List<String> stringList=new ArrayList<>();
        stringList.clear();
        stringList.add("选项一");
        stringList.add("选项二");
        stringList.add("选项三");
        stringList.add("选项四");
        stringList.add("选项五");
        stringList.add("选项六");
        setOnSelectPopListLitner(stringList, new OnSelectPopListLitner() {
            @Override
            public void seleted(String text) {
                ToastUtils.showToast(""+text);
            }
        });
    }


    /**
     * 处理Listview
     * @param contentView
     */
    private void handleListView(View contentView, final EditText mEtName){
        RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.rv_list);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        //加载适配器
        PopListAdapter adapter = new PopListAdapter(context, stringList, R.layout.item_pop_window_text);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener<String>() {
            @Override
            public void onItemClick(View view, int position, String item) {
                mEtName.setText("" + item);
                popWindow.dissmiss();
                if(onSelectPopListLitner!=null){
                    onSelectPopListLitner.seleted(item);
                }
            }
        });
    }

    public interface OnSelectPopListLitner{
        void seleted(String text);
    }

    private List<String> stringList;
    public OnSelectPopListLitner onSelectPopListLitner;
    public void setOnSelectPopListLitner(List<String> stringList,OnSelectPopListLitner listLitner){
        this.stringList = stringList;
        this.onSelectPopListLitner=listLitner;
        mRtlSelect.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                View contentView = LayoutInflater.from(context).inflate(R.layout.pop_layout1,null);
                //处理popWindow 显示内容
                handleListView( contentView,mEtName);
                popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                        .setView(contentView)
                        .setFocusable(true)
                        .setOutsideTouchable(true)
                        .create();
                popWindow.showAsDropDown(mEtName,10,-30);
            }
        });
    }
}

3.item_edittext_select的布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:background="@drawable/gray_border_box"
    android:layout_height="45dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="match_parent">
        <EditText
            android:layout_height="match_parent"
            android:id="@+id/et_name"
            android:singleLine="true"
            android:background="@drawable/seletor_edittext_color_gray2blue"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:textSize="15sp"/>

        <RelativeLayout
            android:id="@+id/rtl_select"
            android:layout_width="50dp"
            android:background="@drawable/gray_border_box"
            android:layout_height="match_parent">
            <ImageView
                android:layout_width="wrap_content"
                android:background="@mipmap/up_down"
                android:layout_centerInParent="true"
                android:layout_centerVertical="true"
                android:layout_height="wrap_content" />
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

4.item_pop_window_text布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="150dp"
    android:layout_height="45dp">
    <TextView
        android:id="@+id/tv_name"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="10dp"
        android:textSize="15sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

你可能感兴趣的:(#,Android之UI相关)