Android开发:PopupWindow实现底部弹出框

项目中需要在某一页实现底部弹出框,于是结合了网上的底部弹出框,自己做了个弹出窗,有点难看,可以根据自己的需要更具体的美化

效果图:



接下来,我们来实现此功能:

直接上代码吧!

1、在点击某个按钮,进行触发,弹出底部框

/**

     * 创建popupWindow

     * popupWindow 是全局定义的,根据自己需要惊醒定义
     * @param view View 比如:btn_ok的点击后触发popupWindow,则view就是id为 btn_ok对应的view
     */
    private void bottomwindow(View view) {
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.pop_five_activity_bottom_layout, null);
        popupWindow = new PopupWindow(layout,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        //点击空白处时,隐藏掉pop窗口
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //添加弹出、弹入的动画
//        popupWindow.setAnimationStyle(R.style.Popupwindow);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.BOTTOM, 0, -location[1]);
        //添加按键事件监听
        setButtonListeners(layout);
        //添加pop窗口关闭事件,主要是实现关闭时改变背景的透明度
//        popupWindow.setOnDismissListener(new poponDismissListener());
//        backgroundAlpha(1f);
    }

/**

     * 设置popupWindow布局中按钮的点击事件

     * p
     * @param view View
     */
    private void setButtonListeners(LinearLayout layout) {
        Button btn_cancel = (Button) layout.findViewById(R.id.btn_cancel);
        Button btn_ok = (Button) layout.findViewById(R.id.btn_ok);
        final EditText et_lat = layout.findViewById(R.id.et_lat);
        final EditText et_lng = layout.findViewById(R.id.et_lng);
        final TextView tv_showResult = layout.findViewById(R.id.tv_showResult);


        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    //在此处添加你的按键处理 xxx
                    popupWindow.dismiss();
                }
            }
        });
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    //在此处添加你的按键处理 xxx
                    String lat = et_lat.getText().toString().trim();
                    String lng = et_lng.getText().toString().trim();
                    if (TextUtils.isEmpty(lat) || TextUtils.isEmpty(lng)) {
                        Toast.makeText(FiveActivity.this, "请填写经纬度", Toast.LENGTH_SHORT).show();
                    } else {
                        long startTime = System.currentTimeMillis();
                        Log.i("startTime:", startTime + "");
                        Double resultDistance = 0.0;
                        Double distance;
                        LatLng startPoint = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
                        List<LatLngDB> latLngDBList = DataSupport.findAll(LatLngDB.class);
                        int i = 0;
                        for (LatLngDB latLngDB : latLngDBList) {
                            Log.i("次数:", "" + i);
                            i++;
                            //计算p1、p2两点之间的直线距离,单位:米
                            LatLng endPoint = new LatLng(latLngDB.getLat(), latLngDB.getLng());
                            distance = DistanceUtil.getDistance(startPoint, endPoint);
                            Log.i("距离:", "" + distance);
                            if (resultDistance < distance) {
                                resultDistance = distance;
                            }
                        }
                        long endTime = System.currentTimeMillis();
                        Log.i("endTime:", endTime + "");
                        long timeCha = endTime - startTime;
                        Toast.makeText(FiveActivity.this, "---" + timeCha + "ms", Toast.LENGTH_SHORT).show();
                        int minute = (int) (timeCha / 60) % 60;
                        int second = (int) (timeCha % 60);
                        if (minute < 10 && second < 10) {
                            tv_showResult.setText("用时 0" + minute + ":0" + second + ";最远距离:" + resultDistance);
                        } else if (minute < 10 && second >= 10) {
                            tv_showResult.setText("用时 0" + minute + ":" + second + ";最远距离:" + resultDistance);
                        } else if (minute >= 10 && second < 10) {
                            tv_showResult.setText("用时 " + minute + ":0" + second + ";最远距离:" + resultDistance);
                        } else if (minute >= 10 && second >= 10) {
                            tv_showResult.setText("用时 " + minute + ":" + second + ";最远距离:" + resultDistance);
                        }
                    }
                }
            }
        });

    }


2、pop_five_activity_bottom_layout  xml布局文件

<?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:background="@drawable/pop_view_bg"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/view_height_25"
            android:layout_margin="@dimen/margin_5"
            android:background="@drawable/pop_add_sub_good_cancel_bg"
            android:text="取消"
            android:textColor="@color/red"
            android:textSize="@dimen/fontsize_12" />

        <View
            android:layout_width="0dp"
            android:layout_height="@dimen/view_height_25"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btn_ok"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/view_height_25"
            android:layout_margin="@dimen/margin_5"
            android:background="@drawable/pop_add_sub_good_cancel_bg"
            android:text="确定"
            android:textColor="@color/red"
            android:textSize="@dimen/fontsize_12" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_margin="@dimen/margin_5"
        android:background="@drawable/pop_view_bg" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="@dimen/margin_10">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="(" />

        <EditText
            android:id="@+id/et_lng"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:hint="经度值" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:text="," />

        <EditText
            android:id="@+id/et_lat"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:hint="纬度值" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text=")" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_showResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/margin_10"
        android:textColor="@color/black"
        android:textStyle="bold" />

</LinearLayout>


3、pop_view_bg 资源文件,在res文件 > 右击 > new > Android resource file > Resource Type 选择 Drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="0.5dp"
        android:color="@color/red"
        android:dashGap="2dp"
        android:dashWidth="4dp" />
    <solid android:color="#F0F0F0" />

</shape>


4、pop_add_sub_good_cancel_bg 资源文件,步骤同 3

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white" />
    <stroke
        android:width="1dp"
        android:color="@color/red" />
    <corners android:radius="2dp" />

</shape>


5、pop_view_bg 资源文件,步骤同 3

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="0.5dp"
        android:color="@color/red"
        android:dashGap="2dp"
        android:dashWidth="4dp" />
    <solid android:color="#F0F0F0" />

</shape>


6、到此,底部弹出框就实现了



适当的放松可以更有效率的学习,扫面下方二维码,关注微信公众号“休闲1刻”,更多精彩等着你



休闲1刻


猜你喜欢

转载自blog.csdn.net/android157/article/details/80595650
今日推荐