1. 程式人生 > >android實現下拉框和輸入框結合

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




相關推薦

easyUI中選擇選單輸入的使用

最近在做一個網站,涉及到需要實現下拉選擇選單和輸入框的混合實現。此時想到了EasyUI上的外掛的使用。 首先下載JQuery EasyUI 登入 http://www.jeasyui.net/download/ 下載EasyUI的GLP版本。    2. 參考程式碼 &l

android實現輸入結合

1、如何實現:將一個EditText和ListView+PopupWindow 結合起來。自定義一個EditText,在自定義控制元件中用PopupWindow實現彈出ListView,已達到想要的效果。 2、需要的佈局: 1、EditText+ImageButton 的佈

android實現(spinner),自定義大小顏色背景位置

1. 實現最簡單的spinner xml檔案,有一個TextView,一個Spinner: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:to

⑨bootstrap組件 按鈕式菜單 輸入 使用基礎案例

opp int sso con utf-8 ref es2017 meta splay View Code <!DOCTYPE html> <html lang="en"> <head> <meta

實現選單多選效果

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <li>工作意願地: <%-- <c:forEach items="${list}" var="lis

Bootstrap -- 選單、輸入組、導航選單

Bootstrap -- 下拉選單、輸入框組、導航選單 1. 下拉選單 可以使用帶有各種大小按鈕的下拉選單:.btn-lg、.btn-sm 或 .btn-xs。 實現下拉選單: <!DOCTYPE html> <html> <head> <

BootStrap之包涵選單的輸入

用法: <div class="row col-md-5 text-center"> <div class="input-group"><!--保持內聯,消除邊框,類

Android簡單實現重新整理重新整理

先把佈局檔案裡面新增一個ListView控制元件, <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.a

Query+ztree實現樹複選功能

完整專案下載連結: CSDN下載頁 檔案比較大是由於該功能是在公司專案中實現的,公司用了AdminLTE.css,其實做這個功能用到的並不多,如果不用該css也沒影響,就是下拉框div會變成圓角,自己改下樣式就行了。在打包上傳的時候我將jquery,boot

bootstrap入門【按鈕式選單,輸入組】

按鈕式下拉選單沒什麼好說的,就是以前學過的東西做了一個巢狀而已。注意dropup即可實現上拉 輸入框組分前面加《span》,後面加和兩頭加三種,還可以新增按鈕,下拉選單,單選複選框哦~ <!DOCTYPE html> <html> <hea

android實現textArea文字域的輸入

在android佈局檔案中實現 Bootstrap 中的 textarea 文字域控制元件,方法如下: 在drawable 資料夾中建立 shape xml 配置檔案 再 xml 檔案中新增如下樣式程式碼 <shape xmlns:android=

選單文字結合

思想: input在最上層 select在最下層 select超出input一個箭頭的長度  在改變select時 通過js將值賦給input 細節: 可以將select的字型顏色換成透明 option換成正常顏色 這樣可以避免bug  fa2這個js 是為了修改文字框後

選單選擇輸入樣式

 <script type="text/javascript" src="js/angular.min.js" ></script>   <style>    .ip {     border: 1px solid red;    }   </style>  &

Android 之WebView實現重新整理其他相關重新整理功能

最近專案中需要用到WebView下拉重新整理的功能,經過查詢資料終於完成了此功能,現在拿出來和大家分享一下。希望對大家有所幫助。 效果如下圖:                        程式碼: activity.xml <?xml version="1

史上最全的使用RecyclerView實現重新整理載入更多

前言:            縱觀多數App,下拉重新整理和上拉載入更多是很常見的功能,但是谷歌官方只有一個SwipeRefreshLayout用來下拉重新整理,上拉載入更多還要自己做。      本篇文章基於RecyclerView簡單封裝了這兩個操作,下拉重

android smartRefresh重新整理載入

1.遠端依賴  compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.5.1' 2.佈局中使用 <com.scwang.smartrefresh.layout.SmartRefreshLayout androi

template-web.js 結合dropload.min.js外掛實現重新整理載入

//引入js,所需要的js已經上傳到個人資源 <script type="text/javascript" src="/web/home/js/template-web.js"></script> <link href="/web/h

Xamarin. Android實現重新整理功能

public class MvxPullToRefreshListView:PullToRefreshListView,PullToRefreshBase.IOnRefreshListener { public MvxPullToRefreshListView(Cont

自定義ListView實現重新整理載入

實現ListView的下拉重新整理和上拉載入,需要先新增headerView和footerView,通過在拖動的過程中,控制頭尾佈局的paddingTop實現。先把paddingTop設為負值,來隱藏header,在下拉的過程中,不斷改變headerView的p

Flutter如何實現重新整理載入更多

效果 下拉重新整理 如果實現下拉重新整理,必須藉助RefreshIndicator,在listview外面包裹一層RefreshIndicator,然後在RefreshIndicator裡面實現onRefresh方法。 body: movie