<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:background="#000000"tools:context="com.example.a2_.MainActivity"><EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#fff" android:text="请输入内容" /><ImageButton android:padding="6dp" android:background="@null" android:layout_alignRight="@id/et_input" android:id="@+id/ib" android:src="@mipmap/down_arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp"android:descendantFocusability="blocksDescendants"android:orientation="horizontal"><ImageView android:src="@mipmap/user" android:id="@+id/iv_user" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:layout_weight="1" android:id="@+id/tv_data" android:text="100" android:textColor="#000000" android:gravity="center_horizontal" android:layout_width="0dp" android:layout_height="wrap_content" /><ImageButton android:background="@null" android:src="@mipmap/delete" android:id="@+id/iv_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
package com.example.a2_;import android.graphics.drawable.ColorDrawable;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.EditText;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.ListView;import android.widget.PopupWindow;import android.widget.TextView;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private EditText et_input;private PopupWindow popupWindow;private ListView lv_list;private ArrayList<String> data;private MyAdapter myAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 et_input = (EditText) findViewById(R.id.et_input); ImageButton ib = (ImageButton) findViewById(R.id.ib); //初始化数据源 initData(); //创建一个listView lv_list = new ListView(this); //给listview设置背景 lv_list.setBackgroundResource(R.mipmap.listview_background); //给listview设置适配器 myAdapter = new MyAdapter(); lv_list.setAdapter(myAdapter); //给listview设置条目点击事件 lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //当点击到一个条目的时候,就把这个条目的内容显示在输入框中 et_input.setText(data.get(position)); popupWindow.dismiss(); } }); //给按钮设置点击事件 ib.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //当点击下拉按钮时,显示列表选项 showPopup(); } });}//初始化数据private void initData() { //创建一个集合,用来存储数据 data = new ArrayList<>(); //通过for循环创建数据 for (int i = 1;i<20;i++){ data.add(100+i+""); }}//显示下拉的列表条目private void showPopup() { //创建PopupWindow对象 popupWindow = new PopupWindow(lv_list,et_input.getWidth(),500,true); //为了在点击列表条目之外的区域能够让列表条目隐藏,为列表条目设置一个背景 popupWindow.setBackgroundDrawable(new ColorDrawable()); //让列表条目显示 并指定在哪个列表条目下展示 popupWindow.showAsDropDown(et_input);}//创建一个适配器class MyAdapter extends BaseAdapter{ @Override public int getCount() { return data.size(); } @Override public String getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { //判断convertView是否为空 if (convertView==null){ convertView = View.inflate(MainActivity.this,R.layout.lv_item,null); } //找到控件 TextView tv_data = (TextView) convertView.findViewById(R.id.tv_data); ImageView iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete); //设置点击事件 iv_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //删除集合里面的数据 data.remove(position); //刷新页面 myAdapter.notifyDataSetChanged(); } }); //获取数据 String data = getItem(position); //设置数据 tv_data.setText(data); return convertView; }}}