温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用popupwindow生成带有列表的对话框,并设置对话框列表的点击事件

发布时间:2020-07-11 07:11:59 来源:网络 阅读:2236 作者:IT学无止境 栏目:移动开发

点击某个View弹出popupwindow列表:

代码:

private ArrayAdapter<String> adapter_huoMing;
private PopupWindow popupWindow;

    /**
     * 这里是popupwindow,用来显示所有查询出来的信息列表
     * 这里的View是调用的时候传入界面中的一个View,popupwindow则显示在此view的下方
     */
    private void showPopupWindow(View view) {

        // 一个自定义的布局,作为显示的内容
        View contentView = LayoutInflater.from(getApplicationContext())
                .inflate(R.layout.activity_popuwindow_huoming, null);
        // 设置按钮的点击事件
        ListView lv_popup = (ListView) contentView
                .findViewById(R.id.lv_popup_huoming);
        //这里R.layout.activity_changnzytd_item为列表中每一项的布局,R.id.tv_changnzytd
          为显示数据的textview,huoMing为列表数据源
        adapter_huoMing = new ArrayAdapter<String>(getApplicationContext(),
                R.layout.activity_changnzytd_item, R.id.tv_changnzytd, huoMing);
        lv_popup.setAdapter(adapter_huoMing);
        lv_popup.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // 这里写Item被点击后的事件
                // Toast.makeText(getApplicationContext(), "1", 0).show();
                if (huoMing.length > 0) {
                    et_huoming.setText(huoMing[position]);
                    popupWindow.dismiss();
                }

            }

        });

        popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话,touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
            }
        });

        // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.abc_btn_borderless_material));

        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);

    }

activity_popuwindow_huoming://这个是要显示在popupwindow中的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv_popup_huoming"
        android:layout_width="fill_parent"
        android:layout_height="250dp" >
    </ListView>

</LinearLayout>


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI