1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android editext下拉框 android实现下拉框和输入框结合

android editext下拉框 android实现下拉框和输入框结合

时间:2023-10-07 10:50:20

相关推荐

android editext下拉框 android实现下拉框和输入框结合

1、如何实现:将一个EditText和ListView+PopupWindow 结合起来。自定义一个EditText,在自定义控件中用PopupWindow实现弹出ListView,已达到想要的效果。

2、需要的布局: 1、EditText+ImageButton 的布局

2、ListView的布局

3、代码

EditText+ImageButton 的布局:

ListView的布局:

自定义控件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 /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();

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。