Android自定义下拉选择框

一、效果:

本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-

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值