下拉选择控件的效果图如下:
要实现这样的效果可以使用PopupWindow来实现,代码如下:
1.MainActivity的代码:
public class MainActivity extends Activity implements OnClickListener{
private EditText editText;
private ImageView iv_select;
private ArrayList<String> list = new ArrayList<String>();
private MyAdapter adapter;
private ListView listView;
private int popupWindowHeight = 300;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initListener();
initData();
}
private void initListener() {
iv_select.setOnClickListener(this);
}
private void initView() {
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
iv_select = (ImageView) findViewById(R.id.iv_select);
}
private void initData() {
for (int i = 0; i < 15; i++) {
list.add(900000+i+"");
}
initListView();
}
private void initListView(){
listView = new ListView(this);
listView.setBackgroundResource(R.drawable.listview_background);
listView.setVerticalScrollBarEnabled(false);//隐藏listview滚动条
adapter = new MyAdapter();
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
editText.setText(list.get(position));
popupWindow.dismiss();
}
});
}
private void showNumberList(){
if(popupWindow==null){
popupWindow = new PopupWindow(listView,
editText.getWidth(), popupWindowHeight);
}
//要让其中的view获取焦点,必须设置为true
popupWindow.setFocusable(true);
//还必须设置一个背景图片
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//设置点击外部消失
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(editText, 0, 0);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_select:
showNumberList();
break;
}
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View view = View.inflate(MainActivity.this, R.layout.adapter_list, null);
TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
ImageButton iv_delete = (ImageButton) view.findViewById(R.id.iv_delete);
tv_number.setText(list.get(position));
iv_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
notifyDataSetChanged();
int listviewHeight = view.getHeight()*list.size();
popupWindow.update(editText.getWidth(), listviewHeight>popupWindowHeight
?popupWindowHeight:listviewHeight);
if(list.size()==0){
popupWindow.dismiss();
iv_select.setVisibility(View.GONE);
}
}
});
return view;
}
}
}
2.布局文件activity_main.xml和adapter_list.xml代码如下:
activity_main.xml:
<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:background="#22000000"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText"
android:singleLine="true"
android:paddingRight="30dp"
android:layout_width="150dp"
android:layout_height="40dp" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_select"
android:layout_alignRight="@id/editText"
android:layout_centerVertical="true"
android:background="@drawable/down_arrow"/>
</RelativeLayout>
</RelativeLayout>
adapter_list.xm:
<?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:gravity="center_vertical"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<TextView android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/user"
android:gravity="center_vertical"
android:drawablePadding="3dp"
android:id="@+id/tv_number"
android:text="90000"/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_delete"
android:background="@drawable/delete"/>
</LinearLayout>
注意点:假如listview的item中有Button,ImageButton,CheckBox等会强制获取焦点的view。 此时,listview的item无法获取焦点,从而无法被点击
解决方法:给item的根布局增加属性: android:descendantFocusability="blocksDescendants" 设置之后,Button获取焦点,item中其他控件也可以获取焦点
Demo地址:http://download.csdn.net/detail/qq_26323183/8807173