listpopwindow实现EditText下拉框显示列表

2019-01-21  本文已影响79人  koinzhang

1月23日更新:博主已实现提示词动态刷新(根据用户输入的关键词,litpopwindow显示关联提示词),可参考利用Adapter动态刷新数据实现联想词提示


这次是为了实现输入关键词,edittext下方显示联想关联词需求,所以用到了ListPopWindow.线上效果图:


效果图

博主为edittext控件设置文字状态监听方法,当输入框文字改变时,显示listpopwindow。同时为listpopwindow的item设置监听方法,当被某item点击时,将item的值传入edittext,同时隐藏listpopwindow。
代码如下:

package com.example.zyk97.infotest.Activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListPopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zyk97.infotest.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CompanySet extends AppCompatActivity {

    @BindView(R.id.back_firstin)
    Button backFirstin;
    @BindView(R.id.confirm_companyset)
    Button confirmCompanyset;
    @BindView(R.id.my_company)
    TextView myCompany;
    @BindView(R.id.input_company)
    EditText inputCompany;
    @BindView(R.id.set_company)
    Button setCompany;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.companyset);
        ButterKnife.bind(this);
        showListPopWindow();
    }
    //显示提示
    public void showListPopWindow() {
        final ListPopupWindow listPopupWindow =new ListPopupWindow(this);
        final String[] list = {"格里", "海尔", "美的"};
        listPopupWindow.setAdapter(new ArrayAdapter<String>(this, R.layout.companyitem, list));//设置适配器
        listPopupWindow.setAnchorView(inputCompany);//设置listpopowindow和哪个控件相邻
        listPopupWindow.setModal(true);//指定listpopwindow是否阻止在显示的时候将内容输入其他窗口


        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                inputCompany.setText(list[position]);
                listPopupWindow.dismiss();//如果已经选择,隐藏listpopwindow
            }
        });
        inputCompany.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (inputCompany.getText().toString().length() > 0) {
                    if(inputCompany.hasFocus()){
                        listPopupWindow.show();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    @OnClick(R.id.set_company)
    public void setMyCompany(View view){
        myCompany.setText(inputCompany.getText());
    }

    //这里使用了AlertDialog
    @OnClick(R.id.business1)
    public void showBusiness1(View view){
        AlertDialog.Builder builder=new AlertDialog.Builder(CompanySet.this);
        builder.setTitle("家电行业公司");
        final String[] cities={"青岛海尔","格力电器","美的集团"};
        builder.setItems(cities, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myCompany.setText(cities[which]);
            }
        });
        builder.show();
    }
}

companyset.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/dark_blue">

        <Button
            android:id="@+id/back_firstin"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="15dp"
            android:layout_gravity="center"
            android:background="@mipmap/back"
            android:textColor="@color/black"
            android:layout_weight="0.01"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="公司设置"
            android:textColor="@color/white"
            android:gravity="center"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/confirm_companyset"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginRight="15dp"
            android:layout_weight="0.01"
            android:background="@color/transparent"
            android:textSize="13sp"
            android:text="确定"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:gravity="center">
        <TextView
            android:id="@+id/my_company"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/transparent"
            android:gravity="center"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:text="我的公司:未设置"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center">
<!--这里使用了自定义的ClearEditText,可以换成EditText!-->
        <com.example.zyk97.infotest.Utils.ClearEditText
            android:id="@+id/input_company"
            android:layout_width="300dp"
            android:layout_height="45dp"
            android:gravity="center_vertical"
            android:paddingLeft="5dp"
            android:drawableLeft="@mipmap/searchcompany"
            android:background="@drawable/button_setting"
            android:hint="请输入公司名称"
            android:textSize="15sp"/>
        <Button
            android:id="@+id/set_company"
            android:layout_width="80dp"
            android:layout_height="45dp"
            android:text="设置"
            android:stateListAnimator="@null"
            android:textColor="@color/white"
            android:background="@color/gray2"/>
    </LinearLayout>
    <TextView
        android:layout_width="380dp"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:layout_gravity="center_horizontal"
        android:text="按行业查找"/>
    <GridLayout
        android:layout_width="380dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:columnCount="2"
        android:rowCount="2">
        <Button
            android:id="@+id/business1"
            android:layout_width="180dp"
            android:layout_height="50dp"
            android:layout_marginBottom="20dp"
            android:layout_marginRight="20dp"
            android:stateListAnimator="@null"
            android:text="家电"
            android:textColor="@color/white"
            android:background="@color/dark_blue"/>
        <Button
            android:id="@+id/business2"
            android:layout_width="180dp"
            android:layout_height="50dp"
            android:background="@color/light_blue"
            android:stateListAnimator="@null"
            android:text="食品"
            android:textColor="@color/white"/>
        <Button
            android:id="@+id/business3"
            android:layout_width="180dp"
            android:layout_height="50dp"
            android:background="@color/light_blue"
            android:stateListAnimator="@null"
            android:text="家具"
            android:textColor="@color/white"/>
        <Button
            android:id="@+id/business4"
            android:layout_width="180dp"
            android:layout_height="50dp"
            android:background="@color/light_blue"
            android:stateListAnimator="@null"
            android:text="汽车"
            android:textColor="@color/white"/>
    </GridLayout>

</LinearLayout>

companyitem.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/company_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:text="default"
    android:paddingLeft="15dp"
    android:textColor="@color/black"
    android:textSize="18sp" />

本文样例完整代码InfoTest


总结一些自己运用listpopwindow时遇到的坑:

  1. listpopwindow不能直接在主线程之中加载,报错Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
    从错误信息我们也可以明白其原因,此问题根本原因就是由于将要弹出的dialog所要依附的View已经不存在导致的。
    可能的情形:
    当界面销毁后再弹出来;或者界面跳转时我们的view发生改变,listpopwindow依附的context发生变化或者界面未运行。

博主在检查代码后,发现是因为自己没注意在主线程中直接加载了listpopwindow,此时界面未运行,listpopwindow找不到依存的view,所以报错。

上一篇下一篇

猜你喜欢

热点阅读