android实现下拉框和输入框结合
来源:互联网 发布:淘宝优惠群起什么名字 编辑:程序博客网 时间:2024/06/14 23:44
1、如何实现:将一个EditText和ListView+PopupWindow 结合起来。自定义一个EditText,在自定义控件中用PopupWindow实现弹出ListView,已达到想要的效果。
2、需要的布局: 1、EditText+ImageButton 的布局
2、ListView的布局
3、代码
EditText+ImageButton 的布局:
<?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="40dp"> <EditText android:id="@+id/CD_Et_account" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginLeft="30dp" android:layout_weight="1" android:background="@null" android:hint="账号" /> <ImageButton android:id="@+id/CD_Iv_drop_image" android:layout_width="40dp" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_marginBottom="2dip" android:layout_marginRight="5dp" android:layout_marginTop="2dip" android:background="@drawable/drop" android:paddingRight="2dip" android:scaleType="fitXY" /></LinearLayout>ListView的布局 :
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#ffffFF" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_marginLeft="20dp" android:id="@+id/CD_pop" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView></LinearLayout>
自定义控件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(); }}
阅读全文
0 0
- android实现下拉框和输入框结合
- 文本和下拉结合的可填可选输入框
- Android 自定义PopupWindow 实现 ListView与PopupWindow结合下拉框
- 下拉和输入框
- dropdownlist textbox 结合,下拉框和文本框结合的控件
- ListView和AsyncTask结合实现下拉刷新
- 可输入下拉框的实现。
- 可输入下拉框的实现
- jsp页面可输入下拉框实现
- html实现可输入下拉框
- select下拉框实现输入功能
- 可以输入的下拉框实现方法
- js实现可输入的下拉框
- 下拉框和输入框的综合
- 下拉框和输入框 求救!
- 可选择和输入的下拉列表框
- 可选择和输入的下拉列表框
- Android自定义Button结合PopupWindow和ListView实现顺序显示下拉菜单控件
- 如何控制SSRS报表中数据的访问权限
- ES5中的函数都是按值传递的
- unit10
- 洛谷P1346 电车
- 模拟实现堆+堆的应用
- android实现下拉框和输入框结合
- hadoop入门九(HDFS基本原理和操作)
- opencv3.1.0 SVM
- [Tensorflow]3.DCGAN代码及实验结果分析
- JS(10)
- 90后分享学习linux系统全过程
- 编辑神器VIM中常用的快捷操作
- NOIP 2003 普及组 数字游戏
- BP神经网络及其Tensorflow实现