android实现下拉框和输入框结合
16lz
2021-01-26
1、如何实现:将一个EditText和ListView+PopupWindow 结合起来。自定义一个EditText,在自定义控件中用PopupWindow实现弹出ListView,已达到想要的效果。
2、需要的布局: 1、EditText+ImageButton 的布局
2、ListView的布局
3、代码
EditText+ImageButton 的布局:
<?xml version="1.0" encoding="utf-8"?>
ListView的布局 :
<?xml version="1.0" encoding="utf-8"?>
自定义控件java代码:
package com.wxcoming.wxcomingerp.custom.sptoedit;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.ColorDrawable;import android.support.annotation.AttrRes;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.text.TextUtils;import android.text.TextWatcher;import android.util.AttributeSet;import android.util.Log;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.EditText;import android.widget.FrameLayout;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.PopupWindow;import com.wxcoming.wxcomingerp.R;/** * Created by adolph_jun on 2017/10/13. */public class CDropEditText extends FrameLayout implements View.OnClickListener, AdapterView.OnItemClickListener { private EditText mEt; private ImageButton mIb; private ListView mLv; private Context context; private PopupWindow popWindow; public CDropEditText(Context context) { super(context); this.context = context; } public CDropEditText(Context context,AttributeSet attrs) { super(context, attrs); //放在三个属性的构造方法处会报错 LayoutInflater.from(context).inflate(R.layout.custome_dropedittext, this); mEt = (EditText) findViewById(R.id.CD_Et_account); mIb = (ImageButton) findViewById(R.id.CD_Iv_drop_image); initPopWindow(mIb); } public CDropEditText( Context context, AttributeSet attrs,int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } @Override protected void onFinishInflate() { super.onFinishInflate(); mIb.setOnClickListener(this); mLv.setOnItemClickListener(this); } /** * 编辑内容改变监听 * @param textWatcher */ public void setEditChange(TextWatcher textWatcher){ if(textWatcher!=null){ mEt.addTextChangedListener(textWatcher); } } /** * 给list添加数据 * @param adapter */ public void setAdapter(BaseAdapter adapter){ //popWindow.showAsDropDown(this, 0, 5); mLv.setAdapter(adapter); } private void initPopWindow(View v) { View view = LayoutInflater.from(getContext()).inflate(R.layout.pop_view, null, false); mLv = (ListView) view.findViewById(R.id.CD_pop); popWindow= new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popWindow.setTouchable(true); popWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } }); popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); //要为popWindow设置一个背景才有效 } public void showPop(){//弹出listview popWindow.showAsDropDown(this, 0, 5); } @Override//下拉图标按钮监听 public void onClick(View v) { if(v.getId() == R.id.CD_Iv_drop_image) { if(popWindow.isShowing()) { popWindow.dismiss(); return; } popWindow.showAsDropDown(this, 0, 5); } } @Override//listview中item的点击事件监听 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mEt.setText(mLv.getAdapter().getItem(position).toString()); popWindow.dismiss(); }}
更多相关文章
- Android(安卓)- UI(User Interface)的基本设计
- android开发中遇到的问题汇总【三】
- Android(安卓)ViewGroup中addView方法使用
- android view滑动的几种方法
- android studio之NDK配置
- android学习01
- android 弹性ScrollView(已优化)
- 《Android(安卓)Activity》活动的介绍和创建
- 利用Hierarchy Viewer优化布局