1. 程式人生 > >android-自定義組合控制元件(EditText+選項)

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>