下拉選擇框,PopuWindows使用

效果圖


Popuwindows.gif
1:布局文件中聲明布局
          <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: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.example.joker.popuwindow.MainActivity">

    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/et_input"
            android:singleLine="true"
            android:layout_width="200dp"
            android:layout_height="wrap_content" />
        <ImageButton
            android:id="@+id/ib_down_arrow"
            android:layout_alignParentRight="true"
            android:src="@drawable/down_arrow"
            android:background="@null"
            android:paddingBottom="6dp"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
             />
    </RelativeLayout>
</RelativeLayout>
2:準(zhǔn)備數(shù)據(jù),準(zhǔn)備ListView
          // 初始化要顯示的內(nèi)容
    private void initListView() {
        listView = new ListView(this);
        listView.setDividerHeight(0);
        listView.setBackgroundResource(R.drawable.listview_background);
        listView.setOnItemClickListener(this);
        
        datas = new ArrayList<String>();
        // 創(chuàng)建一些數(shù)據(jù)
        for (int i = 0; i < 30; i++) {
            datas.add((10000 + i) + "");
        }
        
        listView.setAdapter(new MyAdapter());
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        System.out.println("onItemClick: " + position);
        String string = datas.get(position);
        et_input.setText(string); // 設(shè)置文本
        
        popupWindow.dismiss(); // 消失了
    }
    
    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;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            if(convertView == null){
                view = View.inflate(parent.getContext(), R.layout.item_number, null);
            }else {
                view = convertView;
            }
            
            TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
            tv_number.setText(datas.get(position));
            
            view.findViewById(R.id.ib_delete).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    datas.remove(position);
                    notifyDataSetChanged();
                    
                    if(datas.size() == 0){
                        // 如果刪除的是最后一條, 隱藏popupwindow
                        popupWindow.dismiss();
                    }
                }
            });
            return view;
        }
        
    }

3:創(chuàng)建showPopupWindow方法
            private void showPopupWindow() {
        initListView();
        
        // 顯示下拉選擇框(放入的控件,寬,高)
        popupWindow = new PopupWindow(listView, et_input.getWidth(), 300);
        
        // 設(shè)置點(diǎn)擊外部區(qū)域, 自動(dòng)隱藏
        popupWindow.setOutsideTouchable(true); // 外部可觸摸
        popupWindow.setBackgroundDrawable(new BitmapDrawable()); // 設(shè)置空的背景, 響應(yīng)點(diǎn)擊事件
        
        popupWindow.setFocusable(true); //設(shè)置可獲取焦點(diǎn)
        
        // 顯示在指定控件下
        popupWindow.showAsDropDown(et_input, 0, -5);
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容