Android自定义下拉选择框
来源:互联网 发布:汽车配件软件 编辑:程序博客网 时间:2024/04/29 22:03
一、效果:
本Demo主要是一个自定义的下拉选择框,下拉框可以显示数据,也可以删除数据,类似QQ登陆的EditText。
二、编码
(1)主布局相对比较简单,用一个EditText和ImageButton组合而成,而下拉选择列表是用ListView。
<?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" tools:context="com.project.codingma.customizedselectbox.MainActivity"> <RelativeLayout android:layout_width="200dp" android:layout_height="wrap_content"> <EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageButton android:id="@+id/ib_dropdown" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/et_input" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@null" android:padding="5dp" android:src="@drawable/down_arrow" /> </RelativeLayout></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="match_parent" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/user" /> <TextView android:id="@+id/tv_number" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="1" android:text="12345" /> <ImageButton android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:padding="5dp" android:src="@drawable/delete" /></LinearLayout>
(2)由于ListView中的xml使用了ImageButton,会自动抢夺所在Layout的焦点,导致其他地方的监听事件无效,所以必须在所在Layout添加一个属性。
android:descendantFocusability="blocksDescendants"
(3)绑定相关控件并生成监听事件
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.ib_dropdown).setOnClickListener(this); etInput = (EditText) findViewById(R.id.et_input); } @Override public void onClick(View v) { showPopupWindow(); }
(4)创建PopupWindow对象,并初始化下拉选择框布局和数据
/** * 下拉选择框 */ private void showPopupWindow() { initListView(); //显示下拉框 popupWindow = new PopupWindow(listView, etInput.getWidth(), 300); popupWindow.setOutsideTouchable(true);//外部可触摸 popupWindow.setBackgroundDrawable(new BitmapDrawable()); //设置空的背景 popupWindow.setFocusable(true); //显示在指定控件下 popupWindow.showAsDropDown(etInput, 0, -5); } /** * 下拉选择框数据和视图 */ private void initListView() { listView = new ListView(this); listView.setDividerHeight(0); listView.setBackgroundResource(R.drawable.listview_background); listView.setOnItemClickListener(this); //创建一些数据 datas = new ArrayList<>(); for (int i = 0; i < 30; i++) { datas.add((10000 + i) + ""); } listView.setAdapter(new MyAdapter()); }
private class MyAdapter extends BaseAdapter { @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; }
(5)设置ListView的监听事件
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String s = datas.get(position); etInput.setText(s); popupWindow.dismiss(); }
源码地址:https://github.com/codingma/CustomizedSelectBox-
1 0
- Android自定义下拉选择框
- 自定义下拉选择框
- 自定义下拉选择框
- 自定义下拉选择框
- js 自定义下拉选择框
- android 使用activity自定义下拉选择列表
- 自定义下拉选择框,仿下拉选择框
- Android自定义下拉框
- android 下拉选择框Spinner
- android 下拉选择框Spinner
- js 新版自定义下拉选择框
- 自定义组合控件:下拉选择框
- Android开发自定义下拉框下拉列表
- android中自定义下拉框
- android中自定义下拉框
- android:自定义spinner下拉框
- android Spinner自定义下拉框
- Android 下拉选择
- LeetCode 26 Remove Duplicates from Sorted Array
- struts2的验证
- android_105_侧滑菜单
- Tensorflow tensor操作代码片
- HDU 1003 Max Sum
- Android自定义下拉选择框
- C语言中Stastic变量
- ubuntu16.04中将python3设置为默认
- Altium Designer 16 (AD16) 破解版下载 百度网盘分享 16.1.12 更新
- LeetCode 27 Remove Element
- 通过压力测试提升服务器并发性能实例
- 淘淘商城的实践(6)——单点登录、集群及总结
- 凸包,多边形面积(包装木板,uva 10652)
- LeetCode 28 Implement strStr()