条件选择框

1: 自定义TextView

package com.welleplus.yfinspection.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.TintTypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.welleplus.yfinspection.R;

/**
 * description :
 * Author : Vily
 * Date : 2018/09/11
 * Time : 14:55
 */

public class MyTextView extends LinearLayout {

    private Context mContext;
    private LayoutInflater mInflater;
    private View mView;
    private TextView mTv_title;
    private ImageView mIv_direction;

    public MyTextView(Context context) {
        this(context,null);
        mContext=context;
        mInflater=LayoutInflater.from(context);
    }

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

    }
    @SuppressLint("RestrictedApi")
    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();


        if (attrs != null) {
            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    R.styleable.MyTextView, defStyleAttr, 0);

            //  从属性中获取  rightButtonTitle
            String title = a.getString(R.styleable.MyTextView_text_title);
            if (title != null) {
                setTitle(title);
            }


            //  资源回收
            a.recycle();

        }
    }

    public void setTitle(String title) {

        if(!TextUtils.isEmpty(title)){
            mTv_title.setText(title);
        }
    }

    private void init() {
        if(mView==null){
            mInflater=LayoutInflater.from(getContext());
            mView = mInflater.inflate(R.layout.textview_ui, null);
            mTv_title = mView.findViewById(R.id.tv_title);
            mIv_direction = mView.findViewById(R.id.iv_direction);


            //然后使用LayoutParams把控件添加到子view中
            LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
            addView(mView,params);
        }


    }

    public void setUp(){
        mIv_direction.setImageResource(R.drawable.top);
    }
    public void setDown(){
        mIv_direction.setImageResource(R.drawable.down);
    }
}

2: attrs

 <declare-styleable name="MyTextView">
        <attr name="text_title" format="string"/>

    </declare-styleable>

3: XML 使用

 <com.welleplus.yfinspection.ui.MyTextView
            android:id="@+id/tv_area"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            app:text_title="区域"
            android:tag="area"
            />

4: 代码中点击弹框    自定义弹框  ------->

   List<String> list2 = new ArrayList<>();
                list2.add("全部");
                list2.add("核心设备");
                list2.add("主要设备");
                list2.add("辅助设备");
                if (mPopuWindow == null) {
                    mPopuWindow = new MyPopuWindow(mTvDevice, getActivity(), list2);
                } else {
                    mPopuWindow.setData(mTvDevice, list2);
                }

                mTvDevice.setUp();
                popuClick(mTvDevice);

    private void popuClick(final MyTextView myTextView) {
        if (mPopuWindow != null) {

            mPopuWindow.setOnItemCLickListener(new MyPopuWindow.OnItemCLickListener() {
                @Override
                public void onItemCLick(String item) {
                    mCurrPage = 0;
                    mCacheAdapter.setNewData(null);
                    myTextView.setTitle(item);

                    switch (item) {
                        case "全部":
                            String trim = myTextView.getTag().toString().trim();
                            if(!TextUtils.isEmpty(trim)){
                                switch (trim){
                                    case "area" :
                                        myTextView.setTitle("区域");
                                        mMap.put("area", "");
                                        break;
                                    case "type" :
                                        myTextView.setTitle("设备类型");
                                        mMap.put("etype", "");
                                        break;

                                    default :
                                        break;
                                }
                            }

                            break;

                        case "核心设备":
                            mMap.put("etype", "核心设备");
                            break;
                        case "主要设备":
                            mMap.put("etype", "主要设备");
                            break;
                        case "辅助设备":
                            mMap.put("etype", "辅助设备");
                            break;
                       
                        default:
                            break;
                    }
                    mMap.put("page", mCurrPage + "");
                    requestData(mMap);
                }
            });

            mPopuWindow.setOnPopuDismissListener(new MyPopuWindow.OnPopuDismissListener() {
                @Override
                public void onPopuDismiss() {

                    myTextView.setDown();

                }
            });
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值