一:布局文件
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"/>
二:调用
List list = new ArrayList();
list.add("北京");
list.add("上海");
list.add("武汉");
list.add("南京");
list.add("深圳");
EditText edit = findViewById(R.id.edit);
ListPopupWindow popupWindow = new ListPopupWindow(Activity3.this);
popupWindow.setAdapter(new ArrayAdapter<String>(Activity3.this, android.R.layout.simple_expandable_list_item_1, list));
popupWindow.setAnchorView(edit);
popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setModal(true);
popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
edit.setText(list.get(position).toString());
popupWindow.dismiss();
}
});
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.show();
}
});
三:效果: