【0057】自定义控件-2-组合已有控件实现自定义控件-下拉选择框

1.下拉选择框的效果

2.下拉框的布局

【布局分析】在同一个EditText右侧存在一个ImageButton;

 ImageButton需要响应点击事件;

 

【注意】在ImageButton的位置需要与EditText对齐;但是还需要填充满右侧的点击区域,使用padding选项,否则点击ImageButton的某些区域无法响应;;

3.响应点击事件的popUpWindow

4.listView的生成

5.popWindow的布局

【注意】在delete按钮需要添加padding属性,增加点击区域的有效区域;

 

6.【设置popWindow距离EditText的位置】因此在设置值的时候设置了负值,紧贴位置;

7.去掉分割线,增加listView的背景

7.1增加的背景图片

7.2去掉的分割线

 

7.3 为下拉的item增加点击事件,将item中的内容复制到edit框中

7.3.1 增加监听事件

7.3.2【细节1】

imageButton会抢掉item的点击事件的焦点

 

【修改内容】块焦点的响应就是会响应不同的区域块中的点击事件

 7.3.3【细节2】popUpWindow默认不可以响应点击事件

7.3.4【注意】

【修改】

8.点击外部域使得popUpWindow隐藏掉

 9.源码

【MainActivity源码】

  1 package com.itheima74.popupwindow;
  2 
  3 import java.util.ArrayList;
  4 
  5 import android.app.Activity;
  6 import android.graphics.drawable.BitmapDrawable;
  7 import android.os.Bundle;
  8 import android.view.View;
  9 import android.view.View.OnClickListener;
 10 import android.view.ViewGroup;
 11 import android.widget.AdapterView;
 12 import android.widget.AdapterView.OnItemClickListener;
 13 import android.widget.BaseAdapter;
 14 import android.widget.EditText;
 15 import android.widget.ListView;
 16 import android.widget.PopupWindow;
 17 import android.widget.TextView;
 18 
 19 public class MainActivity extends Activity implements OnClickListener, OnItemClickListener{
 20 
 21     private ListView listView;
 22     private EditText et_input;
 23     private ArrayList<String> datas;
 24     private PopupWindow popupWindow;
 25 
 26     @Override
 27     protected void onCreate(Bundle savedInstanceState) {
 28         super.onCreate(savedInstanceState);
 29         setContentView(R.layout.activity_main);
 30         
 31         findViewById(R.id.ib_dropdown).setOnClickListener(this);
 32         
 33         et_input = (EditText) findViewById(R.id.et_input);
 34     }
 35 
 36     @Override
 37     public void onClick(View v) {
 38         showPopupWindow();
 39     }
 40 
 41     private void showPopupWindow() {
 42         initListView();
 43         
 44         // 显示下拉选择框
 45         popupWindow = new PopupWindow(listView, et_input.getWidth(), 300);
 46         
 47         // 设置点击外部区域, 自动隐藏
 48         popupWindow.setOutsideTouchable(true); // 外部可触摸
 49         popupWindow.setBackgroundDrawable(new BitmapDrawable()); // 设置空的背景, 响应点击事件
 50         
 51         popupWindow.setFocusable(true); //设置可获取焦点
 52         
 53         // 显示在指定控件下
 54         popupWindow.showAsDropDown(et_input, 0, -5);
 55     }
 56 
 57     // 初始化要显示的内容
 58     private void initListView() {
 59         listView = new ListView(this);
 60         listView.setDividerHeight(0);
 61         listView.setBackgroundResource(R.drawable.listview_background);
 62         listView.setOnItemClickListener(this);
 63         
 64         datas = new ArrayList<String>();
 65         // 创建一些数据
 66         for (int i = 0; i < 30; i++) {
 67             datas.add((10000 + i) + "");
 68         }
 69         
 70         listView.setAdapter(new MyAdapter());
 71     }
 72     
 73     @Override
 74     public void onItemClick(AdapterView<?> parent, View view, int position,
 75             long id) {
 76         System.out.println("onItemClick: " + position);
 77         String string = datas.get(position);
 78         et_input.setText(string); // 设置文本
 79         
 80         popupWindow.dismiss(); // 消失了
 81     }
 82     
 83     class MyAdapter extends BaseAdapter {
 84 
 85         @Override
 86         public int getCount() {
 87             return datas.size();
 88         }
 89 
 90         @Override
 91         public Object getItem(int position) {
 92             return datas.get(position);
 93         }
 94 
 95         @Override
 96         public long getItemId(int position) {
 97             return position;
 98         }
 99 
100         @Override
101         public View getView(final int position, View convertView, ViewGroup parent) {
102             View view;
103             if(convertView == null){
104                 view = View.inflate(parent.getContext(), R.layout.item_number, null);
105             }else {
106                 view = convertView;
107             }
108             
109             TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
110             tv_number.setText(datas.get(position));
111             
112             view.findViewById(R.id.ib_delete).setOnClickListener(new OnClickListener() {
113                 
114                 @Override
115                 public void onClick(View v) {
116                     datas.remove(position);
117                     notifyDataSetChanged();
118                     
119                     if(datas.size() == 0){
120                         // 如果删除的是最后一条, 隐藏popupwindow
121                         popupWindow.dismiss();
122                     }
123                 }
124             });
125             return view;
126         }
127         
128     }
129 
130 
131     
132 }

【activity_main.xml布局源码】

 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     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10 
11     <RelativeLayout
12         android:layout_width="200dp"
13         android:layout_height="wrap_content" >
14 
15         <EditText
16             android:id="@+id/et_input"
17             android:layout_width="match_parent"
18             android:layout_height="wrap_content" />
19 
20         <ImageButton
21             android:id="@+id/ib_dropdown"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:layout_alignParentRight="true"
25             android:layout_alignParentTop="true"
26             android:layout_alignBottom="@id/et_input"
27             android:background="@null"
28             android:padding="5dp"
29             android:src="@drawable/down_arrow" />
30     </RelativeLayout>
31 
32 </RelativeLayout>

 

【item_number.xml源码源码】

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:padding="3dp"
 6     android:descendantFocusability="blocksDescendants"
 7     android:gravity="center_vertical"
 8     android:orientation="horizontal" >
 9 
10     <ImageView
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:src="@drawable/user" />
14 
15     <TextView
16         android:id="@+id/tv_number"
17         android:layout_width="0dp"
18         android:layout_height="wrap_content"
19         android:layout_weight="1"
20         android:layout_marginLeft="10dp"
21         android:text="12306" />
22 
23     <ImageButton
24         android:id="@+id/ib_delete"
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:padding="5dp"
28         android:background="@null"
29         android:src="@drawable/delete" />
30 
31 </LinearLayout>

 

posted @ 2018-01-05 22:11  OzTaking  阅读(524)  评论(0)    收藏  举报