下拉列表

下拉列表

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  2  xmlns:tools="http://schemas.android.com/tools"  3  android:layout_width="match_parent"  4  android:layout_height="match_parent" >  5  6 <EditText  7 android:id="@+id/input"  8  android:layout_width="200dp"  9  android:layout_height="wrap_content" 10  android:layout_centerHorizontal="true" 11  android:layout_marginTop="10dp" 12  android:text="@string/hello_world" /> 13 14 <!-- 下啦三角图案 --> 15 <ImageView 16 android:id="@+id/down_arrow" 17  android:layout_width="wrap_content" 18  android:layout_height="wrap_content" 19  android:background="@drawable/down_arrow" 20  android:layout_alignRight="@id/input" 21  android:clickable="true" 22  android:layout_marginTop="20dp" /> 23 </RelativeLayout>

 

  1 package com.example.testdemo;  2  3 import java.util.ArrayList;  4 import java.util.List;  5 import android.app.Activity;  6 import android.os.Bundle;  7 import android.view.View;  8 import android.view.View.OnClickListener;  9 import android.view.ViewGroup;  10 import android.widget.BaseAdapter;  11 import android.widget.EditText;  12 import android.widget.ImageView;  13 import android.widget.ListView;  14 import android.widget.PopupWindow;  15 import android.widget.TextView;  16  17 public class MainActivity extends Activity {  18  19 private EditText et_input;  20 private ImageView downArrow;  21 private List<String> msgList;  22 private PopupWindow popWin;  23 private ListView listView;  24  25  @Override  26 protected void onCreate(Bundle savedInstanceState) {  27 super.onCreate(savedInstanceState);  28  setContentView(R.layout.activity_main);  29  30 et_input = (EditText) findViewById(R.id.input);  31 downArrow = (ImageView) findViewById(R.id.down_arrow);  32 msgList = new ArrayList<String>();  33  34 for (int i = 0; i < 20; i++) {  35 msgList.add("1000000000" + i);  36  }  37  38  initListView();  39  40 downArrow.setOnClickListener(new OnClickListener() {  42  @Override  43 public void onClick(View v) {  44 System.out.println("=======");  45 // 定义 popupWindow  46 popWin = new PopupWindow(MainActivity.this);  47 // 设置宽度  48  popWin.setWidth(et_input.getWidth());  49 // 设置popWin 高度  50 popWin.setHeight(200);  51 // 为popWindow填充内容  52  popWin.setContentView(listView);  53 // 点击popWin 以处的区域,自动关闭 popWin  54 popWin.setOutsideTouchable(true);  55 // 设置 弹出窗口,显示的位置  56 popWin.showAsDropDown(et_input, 0, 0);  57  }  58  });  59  }  60  61 private void initListView() {  62 listView = new ListView(this);  63 // 设置listView背景  64  listView.setBackgroundResource(R.drawable.listview_background);  65 // 设置条目之间的分隔线为null  66 listView.setDivider(null);  67 // 关闭  68 listView.setVerticalScrollBarEnabled(false);  69 listView.setAdapter(new MyListAdapter());  70  }  71  72 private class MyListAdapter extends BaseAdapter {  73  @Override  74 public int getCount() {  75 return msgList.size();  76  }  77  @Override  78 public Object getItem(int position) {  79 return position;  80  }  81  @Override  82 public long getItemId(int position) {  83 return position;  84  }  86  @Override  87 public View getView(final int position, View convertView,  88  ViewGroup parent) {  89  ViewHolder holder;  90 if (convertView == null) {  91 convertView = View.inflate(getApplicationContext(),  92 R.layout.list_item, null);  93 holder = new ViewHolder();  94  95 holder.delete = (ImageView) convertView  96  .findViewById(R.id.delete);  97 holder.tv_msg = (TextView) convertView  98  .findViewById(R.id.tv_list_item);  99  convertView.setTag(holder); 100 } else { 101 holder = (ViewHolder) convertView.getTag(); 102  } 103  holder.tv_msg.setText(msgList.get(position)); 104 holder.delete.setOnClickListener(new OnClickListener() { 106  @Override 107 public void onClick(View v) { 108 // 删除对应的条目 109  msgList.remove(position); 110 // 刷新listView 111 MyListAdapter.this.notifyDataSetChanged(); 112  } 113  }); 114 convertView.setOnClickListener(new OnClickListener() { 115  @Override 116 public void onClick(View v) { 117 // 设置输入框 118  et_input.setText(msgList.get(position)); 119  popWin.dismiss(); 120  } 121  }); 122 return convertView; 123  } 124  } 125 126 private class ViewHolder { 127  TextView tv_msg; 128  ImageView delete; 129  } 130 131 }

 

你可能感兴趣的:(下拉列表)