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

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();
    }
}








  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,下拉框输入框都是常用的UI组件。下面分别介绍它们的实现方法: 1. 下拉框(Spinner) 下拉框是用来供用户选择一个或多个选项的控件,通常用于表单中的选项选择。 在XML布局文件中,可以用Spinner标签来定义一个下拉框: ``` <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 接着,在Java代码中需要为Spinner设置一个Adapter,来显示下拉框中的选项。下面是示例代码: ``` Spinner spinner = findViewById(R.id.spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.options, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); ``` 其中,R.array.options是存储选项文本的字符串数组,可以在res/values/strings.xml文件中定义,如下: ``` <string-array name="options"> <item>选项1</item> <item>选项2</item> <item>选项3</item> </string-array> ``` 2. 输入框(EditText) 输入框是用来供用户输入文本内容的控件,通常用于表单中的文本输入。 在XML布局文件中,可以用EditText标签来定义一个输入框: ``` <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入文本" /> ``` 接着,在Java代码中可以通过findViewById()方法获取输入框对象,并对其进行操作,如下: ``` EditText editText = findViewById(R.id.edit_text); String text = editText.getText().toString(); // 获取输入框中的文本内容 editText.setText("Hello world!"); // 设置输入框的文本内容 ``` 除此之外,还可以为EditText设置输入类型、最大长度、输入提示等属性,具体可查看Android官方文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值