输入框的下拉联想list
# 输入框的下拉联想列表pop实现
基于ListPopupWindow. 实现popupWindow不被挤开,悬浮于软键盘之上的效果
editText.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
super.onTextChanged(s, start, before, count);
String str = s.toString();
if(pop != null){
pop.dismiss();
pop = null;
}
if(str.endsWith("@")){
showPopEmail(str);
}
}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ListPopupWindow pop;
private void showPopEmail(String str) {
ArrayList<String> mArrayList=new ArrayList<String>();
mArrayList.add("@gmail.com");
mArrayList.add("@qq.com");
mArrayList.add("@163.com");
mArrayList.add("@hotmail.com");
mArrayList.add("@yahoo.com");
mArrayList.add("@77.net");
mArrayList.add("@iooo.com");
mArrayList.add("@xx.com");
mArrayList.add("@yy.com");
mArrayList.add("@77.com");
pop = new ListPopupWindow(getContext());
ListPopupWindowAdapter adapter = new ListPopupWindowAdapter(mArrayList,getContext());
pop.setAdapter(adapter);
pop.setWidth(editText.getMeasuredWidth());
pop.setHeight(LayoutParams.WRAP_CONTENT);
pop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
String chosen = mArrayList.get(position);
Toast.debug(chosen);
String s = str+chosen.substring(1);
mClearHideEditText.setText(s);
mClearHideEditText.setSelection(s.length());
if(pop != null){
pop.dismiss();
}
}
});
pop.setAnchorView(mClearHideEditText);
pop.setDropDownGravity(Gravity.BOTTOM);
//关键点1: 设置popupwindow的软键盘模式
pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
pop.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); //设置高度自适应!
pop.show();
if(pop.getListView() != null){
pop.getListView().setBackgroundResource(R.drawable.bg_edit_shape);
pop.getListView().setVerticalScrollBarEnabled(false);
}
Activity activity = ContextUtil.getActivityFromContext(getContext());
if(activity == null || activity.getWindow() ==null){
return;
}
//关键点2: 设置activity的软键盘模式
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
}
static Activity getActivityFromContext(Context context) {
if (context == null) {
return null;
}
if (context instanceof Activity) {
return (Activity) context;
}
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity) context;
}
context = ((ContextWrapper) context).getBaseContext();
}
return null;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
编辑 (opens new window)
上次更新: 2022/08/25, 20:20:31