效果图
步骤:
1.画出编辑框的布局、popupWindow的布局、popupWindow中listview每行的布局
2.new一个PopupWindow对象,设置其属性
3.定义一个BaseAdapter的继承类,将数据添加到ListView中,最后添加监听事件
*编辑框的布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context="org.mobiletrain.a7_4popupwindow.MainActivity"> <EditText
android:id="@+id/et"
android:layout_width="300dp"
android:layout_height="48dp"
android:hint="请输入用户名"/> <ImageView
android:id="@+id/show_pw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/et"
android:onClick="showPw"
android:src="@drawable/down_arrow"/>
</RelativeLayout>
*popupWindow中的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
*popupWindow中的listview中每个元素的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"> <ImageView
android:id="@+id/userface"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="12dp"
android:src="@drawable/user"/> <TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_centerInParent="true"
android:gravity="center"
android:text="username"/> <ImageView
android:id="@+id/delete"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:padding="12dp"
android:src="@drawable/delete"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity { private EditText et;
private List<String> list;
private PopupWindow pw; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = ((EditText) findViewById(R.id.et));
} public void showPw(View view) {
//1.PopupWindow即将显示的布局
//2.popupwindow的宽度
//3.popupwindow的高度
View contentView = LayoutInflater.from(this).inflate(R.layout.pw_layout, null);
initListView(contentView);
pw = new PopupWindow(contentView, et.getWidth(), 400);
//设置当点击其他区域时关闭PopupWindow
pw.setOutsideTouchable(true);
pw.setBackgroundDrawable(new BitmapDrawable());
//设置让PopupWindow获取焦点,如果不设置,在PW打开的情况下点击后退按钮会直接退出应用
pw.setFocusable(true); //显示在某一个控件的下方,后面两个参数表示x,y轴 的偏移量
pw.showAsDropDown(et, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -16, getResources().getDisplayMetrics()), 0);
} //初始化popupWindow中的listview
private void initListView(View contentView) {
ListView listView = (ListView) contentView.findViewById(R.id.lv);
list = getData();
MyAdapter adapter = new MyAdapter(this, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
et.setText(list.get(position));
//关闭弹出框
pw.dismiss();
}
});
} private List<String> getData() {
List<String> list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
list.add("李四:" + i);
}
return list;
}
}
public class MyAdapter extends BaseAdapter {
private Context context;
private List<String> list;
private LayoutInflater inflater; public MyAdapter(Context context, List<String> list) {
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return list.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listview_item, null);
holder = new ViewHolder();
holder.delete = (ImageView) convertView.findViewById(R.id.delete);
holder.username = (TextView) convertView.findViewById(R.id.username);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.username.setText(list.get(position));
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
class ViewHolder{
TextView username;
ImageView delete;
}
}