最近要实现一个需求:根据edittext输入框内的关键词,动态的在listpopwindow中显示联想词,利用listpopwindow实现下拉框提示,可以参考我的另一篇文章listpopwindow实现EditText下拉框显示列表。
先上效果图
博主的思路是为edittext设置文字变化监听addTextChangedListener,在onTextChanged方法,实现关键词判断(判断出应该显示哪些提示词),然后将新的提示词放进matchList集合中,通知adapter刷新view。
关键代码:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (inputCompany.getText().toString().length() > 0) {
setCompany.setBackgroundColor(getColor(R.color.dark_blue));
//这是博主自己写的工具类,用来判断包含用户输入的关键词的提示词
KeyWordMatch keyWordMatch = new KeyWordMatch();
List<String> ml = keyWordMatch.getMatch(list,s.toString());//新的提示词集合
matchList.clear();//清除原有matchList集合
matchList.addAll(ml);//将新的提示词赋值给matchList
adapter.notifyDataSetChanged();//通知view数据源改变,刷新biew
listPopupWindow.show();//显示listpopwindow
}
}
下面贴出实现这个功能所需的代码,包括一个主activity:CompanySet.java;主activity的布局文件:companyset.xml;一个listpopwinodw的item的布局文件:companyitem.xml;一个工具类,用来判断应显示的提示词:KeyWordMatch.java
CompanySet.java
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 com.example.zyk97.infotest.Utils.KeyWordMatch;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class CompanySet extends AppCompatActivity {
@BindView(R.id.back)
Button back;
@BindView(R.id.confirm)
Button confirm;
@BindView(R.id.my_company)
TextView myCompany;
@BindView(R.id.input)
EditText inputCompany;
@BindView(R.id.set)
Button setCompany;
private List<String> list=new ArrayList<>();
// private static String[] matchList = {"格力", "海尔", "美的", "小米", "讯飞"};
private List<String> matchList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.companyset);
ButterKnife.bind(this);
list.add("海尔");
list.add("格力");
list.add("美的");
list.add("讯飞");
list.add("大疆");
list.add("奎格");
showListPopWindow();
}
@OnClick(R.id.back)
public void setBack(View view) {
finish();
}
@OnClick(R.id.confirm)
public void setConfirm(View view) {
finish();
}
@OnClick(R.id.my_company)
public void resetMyCompany() {
myCompany.setText("我的公司:未设置");
}
//显示提示词
public void showListPopWindow() {
final ListPopupWindow listPopupWindow = new ListPopupWindow(this);
final ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.companyitem, matchList);
listPopupWindow.setAdapter(adapter);//设置适配器,使用默认的适配器
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(matchList.get(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) {
setCompany.setBackgroundColor(getColor(R.color.dark_blue));
KeyWordMatch keyWordMatch = new KeyWordMatch();
List<String> ml = keyWordMatch.getMatch(list,s.toString());
matchList.clear();
matchList.addAll(ml);
adapter.notifyDataSetChanged();
listPopupWindow.show();
}
}
@Override
public void afterTextChanged(Editable s) {
if (inputCompany.getText().toString().length() == 0) {
setCompany.setBackgroundColor(getColor(R.color.gray2));
}
}
});
}
@OnClick(R.id.set)
public void setMyCompany(View view) {
myCompany.setText(inputCompany.getText());
}
@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();
}
@OnClick(R.id.business2)
public void showBusiness2(View view) {
Toast.makeText(CompanySet.this, "行业待开通!", Toast.LENGTH_SHORT).show();
}
@OnClick(R.id.business3)
public void showBusiness3(View view) {
Toast.makeText(CompanySet.this, "行业待开通!", Toast.LENGTH_SHORT).show();
}
@OnClick(R.id.business4)
public void showBusiness4(View view) {
Toast.makeText(CompanySet.this, "行业待开通!", Toast.LENGTH_SHORT).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"
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"
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:textColor="@color/white"
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">
<com.example.zyk97.infotest.Utils.ClearEditText
android:id="@+id/input"
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"
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="40dp"
android:layout_margin="20dp"
android:text="default"
android:paddingLeft="15dp"
android:textColor="@color/black"
android:gravity="center_vertical"
android:textSize="18sp" />
KeyWordMatch.java
package com.example.zyk97.infotest.Utils;
import java.util.ArrayList;
import java.util.List;
public class KeyWordMatch {
public void KeyEordMatch() {
}
;
/**
* @param list 主串数组list
* @param sub 字串
* @return 返回一个匹配完成的数组list
*/
public List<String> getMatch(List<String> list, String sub) {
List<String> matchList = new ArrayList<>();
for (int i = 0; i <list.size(); i++) {
if (list.get(i).contains(sub)) {
matchList.add(list.get(i));
}
}
return matchList;
}
}
本文样例完整代码InfoTest
网友评论