PopupWindow的使用和两个案例

Android进阶之路 - popupWindow使用详解 popupWindow还是比较好用的一款控件,他不会像Dialog一样让内容与样式死板,他的优势就在于我们可以填充自己喜欢的样式;在使用中遮盖层是个不大不小的问题,本文已经解决,同时还有一些其他问题的解决方式,也都在代码中进行了注释,大家请直接往下看吧(PS:最后又惊喜,或许你想要的答案就在最后哦) 阅读详情

PopupWindow在安卓中使用很广, 如微信的点赞等等跳出的窗体

下面看实例一:



不多说,直接上代码:

 private void AreaPopupWindow() {
        if(mPopupWindow == null) {

            View areaContentView = createAreaContentView();
            int width = mActivity.getWindowManager().getDefaultDisplay().getWidth();
            int height = ViewGroup.LayoutParams.WRAP_CONTENT;
            boolean focusable = true;
            mPopupWindow = new PopupWindow(areaContentView, width, height, focusable);
            mPopupWindow.setBackgroundDrawable(new ColorDrawable());  //点击返回键要让popupwindow消失需要给它设置一个背景
            mPopupWindow.setOutsideTouchable(true);
        }

        //显示Popupwindow
        View anchor = mTvWangdianqiandaoAreaPopup;   //指定popupWindow显示在什么控件的下方
        int xoff = 2;	// 指定PopupWindow x方向的偏移量
        int yoff = 40;	// 指定PopupWindow y方向的偏移量
        mPopupWindow.showAsDropDown(anchor,xoff,yoff);


    }

    /** 创建地区PopupWindow显示的内容 */
    private View createAreaContentView() {

        View contentView =  LayoutInflater.from(mActivity).inflate(R.layout.view_qingwuexcute_area_popup,null);
        final ListView area1 = (ListView) contentView.findViewById(R.id.lv_area1);
        final ListView area2 = (ListView) contentView.findViewById(R.id.lv_area2);
        area1.setVerticalScrollBarEnabled(false);  //隐藏两个条目右边的滑动条
        area2.setVerticalScrollBarEnabled(false);
        area2.setVisibility(View.INVISIBLE);
        mWangDianArea2Adapter = new WangDianArea2Adapter();
        area2.setAdapter(mWangDianArea2Adapter);   //给2设置适配器

        final WangDianArea1Adapter wangDianArea1Adapter = new WangDianArea1Adapter();
        area1.setAdapter(wangDianArea1Adapter);

        area1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //选中更改area1中条目的背景色  和文字的颜色
                //将点击条目的位置 传给area1的adapter
                wangDianArea1Adapter.setItemClickPosition(position);

                //给ListView2 加一个动画
                area2.setVisibility(View.VISIBLE);
                ObjectAnimator animator1 = ObjectAnimator.ofFloat(area2, "translationY", -100.0f, 0);
                AnimatorSet set = new AnimatorSet();
                set.play(animator1);
                set.setDuration(300);
                set.start();

                //这里处理  点击左边的ListView的条目,让右边的ListView刷新集合  (第二种实现方式:让右边ListView滚动到对应的条目显示)
                mDifferentWangDianQianDaoArea2Beens = AllArea2Lists.get(position);
                mWangDianArea2Adapter.updataRightData(mDifferentWangDianQianDaoArea2Beens);
                area2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //更改area2中选中的条目的  颜色和对号的显示
                        //循环条目
                        for(int i = 0; i < mDifferentWangDianQianDaoArea2Beens.size();i++) {
                            WangDianQianDaoArea2Bean area2Bean = mDifferentWangDianQianDaoArea2Beens.get(i);
                            area2Bean.isChecked = false;
                            if(position == i) {
                                area2Bean.isChecked = true;
                            }
                        }
                        mWangDianArea2Adapter.notifyDataSetChanged();
                        //更改上方区域中的内容
                        mTvWangdianqiandaoAreaPopup.setText(mDifferentWangDianQianDaoArea2Beens.get(position).area2);
                        //保存区域中第一个条目到sp中
                        SharedPreferences sp = mActivity.getSharedPreferences("hz", Activity.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("wangdianqiandao_area",mDifferentWangDianQianDaoArea2Beens.get(position).area2);
                        editor.commit();
                        mPopupWindow.dismiss();    //让悬浮窗体消失
                    }
                });

            }
        });
        return contentView;


    }

这个例子, 是PopupWindow显示在什么控件的下方,并且距离控件的偏移量可以设定;  注意点击popupWindow 和返回键可以让PopupWindow消失的写法


实例二:



这个Demo弹出的是PopupWindow, 上代码:

package com.example.popupwindowpaydemo.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.popupwindowpaydemo.BottomDialogListener;
import com.example.popupwindowpaydemo.R;

/**
 * 底部支付弹窗的Popupwindow
 */

public class MyBottomPopupWindow extends PopupWindow implements View.OnClickListener {

    public BottomDialogListener mListener;
    private final TextView mTv_back;
    private final EditText mEt_input;
    private final Button mBt_cancel;
    private final Button mBt_confirm;

    private Context mContext;
    private final View mView;


    public MyBottomPopupWindow(Context context,BottomDialogListener listener) {
       this.mListener = listener;
        this.mContext = context;

        //PopupWindow显示的内容
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = layoutInflater.from(context).inflate(R.layout.view_mypopupwindow,null);
        mTv_back = (TextView) mView.findViewById(R.id.tv_back);
        mTv_back.setOnClickListener(this);
        mEt_input = (EditText) mView.findViewById(R.id.et_input);
        mBt_cancel = (Button) mView.findViewById(R.id.bt_cancel);
        mBt_confirm = (Button) mView.findViewById(R.id.bt_confirm);
        mBt_cancel.setOnClickListener(this);
        mBt_confirm.setOnClickListener(this);

        this.setContentView(mView);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                this.dismiss();
                break;
            case R.id.bt_cancel:
                this.dismiss();
                break;
            case R.id.bt_confirm:
                String str = mEt_input.getText().toString().trim();
                if(TextUtils.isEmpty(str)) {
                    Toast.makeText(mContext,"密码为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                //调用接口中的方法 回调给页面
                if(mListener != null) {
                    mListener.onDataToActivity(str,mView);   //为什么要讲view传过去 ?
                    dismiss();
                }
                break;
        }
    }
}
继承了PopupWindow, 对属性进行设置,  并且加入接口回调;  

这个弹出窗体的布局有点奥妙:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical">

            <RelativeLayout

                android:layout_width="match_parent"
                android:layout_height="40dp">

                <TextView
                    android:id="@+id/tv_back"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:padding="5dp"
                    android:text="→"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="请输入交易密码"/>

            </RelativeLayout>

            <EditText
                android:id="@+id/et_input"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="请输入密码"/>

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

                <Button
                    android:id="@+id/bt_cancel"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="取消"/>

                <Button
                    android:id="@+id/bt_confirm"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="确认"/>

            </LinearLayout>

        </LinearLayout>
    </RelativeLayout>
</ScrollView>
整体用ScrollView进行包装, 弹窗弹起的时候,软键盘一般是会覆盖一部分输入框的,这是非常不好看的。之所以demo中没有出现这种情况,是通过了特殊的处理——>将整个弹窗布局外包一层ScrollView,这样方便软键盘弹起找到输入框焦点时会将整个布局往上顶。但,这还不够,一定要在ScrollView中设置一个属性:android:fillViewport="true"  

MainActivity中的代码:

package com.example.popupwindowpaydemo;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.example.popupwindowpaydemo.view.MyBottomPopupWindow;

public class MainActivity extends AppCompatActivity {

    private TextView tv_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_pwd = (TextView) findViewById(R.id.tv_pwd);


    }

    public void click(View view) {
        MyBottomPopupWindow myBottomPopupWindow = new MyBottomPopupWindow(this, new BottomDialogListener() {
            @Override
            public void onDataToActivity(String str, View v) {
                //回调 回来的数据 ,由于接口中已经做了让窗体消息的处理,故这里不做处理
                tv_pwd.setText(str);

            }
        });

        myBottomPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        myBottomPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        myBottomPopupWindow.setOutsideTouchable(false);
//        myBottomPopupWindow.setAnimationStyle(R.style.DialogShowStyle);
        myBottomPopupWindow.setBackgroundDrawable(new ColorDrawable());
        myBottomPopupWindow.setFocusable(true);

        //设置PopupWIndow显示的位置, 设置窗口显示在parent布局的位置显示
        myBottomPopupWindow.showAtLocation(this.findViewById(R.id.activity_main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0,0);
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);//自动打开软键盘
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

    }
}
popupWindow 设置setFocusable为true, 才能让Edittext获得焦点 输入密码;同时 这里可以设置一个背景色,由于他是包含整个父窗体的

注意showAtLoacation()和showAsDropDown()的用法




深入掌握PopupWindow的实用技巧和案例教程 本文还有配套的精品资源,点击获取 简介:PopupWindow是Android中用于创建轻量级弹窗的组件,可以用于实现下拉菜单和提示信息等。该教程深入讲解了PopupWindow的创建、基本使用方法、特性以及如何在项目中灵活应用。学习内容包括PopupWindow的创建和核心参数设置、显示位置的调整、关键属性配置如背景透明度和外部触摸响应、动画效果的添加,以及与监听事件的结... 阅读详情

相关推荐

Android简易PopupWindow Tooltip库的实现与应用

PopupWindow在Android应用中是一个非常实用的组件,它允许开发者在界面上弹出一个浮动窗口,用于显示额外的信息或者作为交互菜单使用。它是建立在WindowManager服务之上的,因此可以提供较高的自定义性和灵活性。与Toast相比,PopupWindow能够提供更丰富的交互,比如点击内部视图可以进行响应,甚至可以包含复杂的布局和控件。PopupWindow的基本原理是使用View作为内容视图,通过WindowManager将该View显示在屏幕上的特定位置。

weixin_33628677的博客 1082

PopupWindow案例 PopUpWindow+ListView 使用

1. PopupWindow Api理解: PopupWindow popupWindow=null; View pView=null; public void showPopWindow(View view){ if(popupWindow==null){ // 加载布局 pView =View.inflate(this,R.layout.popup...

dreams_deng的博客 1029

popupwindow项目使用案例

popupwindow项目使用案例,欢迎大家指正

【Android】popup windows 的使用方式 和 遇到不显示的坑

在项目开发过程中有一个需求就是点击一个问号icon 弹出相关提示信息在下面,那么就得对这个做适配了。

weixin_44002043的博客 1904

popupwindow使用案例

popupwindow的简单使用,对提示框的显示位置做了详细描述,和用popupwindow做简单的下拉列表。

Android自定义PopupWindow小案例

主要为大家详细介绍了Android自定义PopupWindow小案例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

PopupWindow的使用例子

先上图 用的豌豆荚截图,本来是个动画的,每个过程都有几张,大概就是要实现这个效果,初始状态只有一个Button,当点击show的时候,另外一个页面从底部慢慢升起来,直到覆盖到上一个页面,注意这里不是启用另一个Activity,是用的PopupWindow,当点击dismiss的时候,又慢慢消失。。。这种效果看上去不错,PopupWind

Qiqihar的专栏 1012

PopupWindow弹出窗口案例

PopupWindow弹出窗口案例源码,最近做一个项目用到了popupWindow,遇到一个问题,当PopupWindow向下弹出时的界面空间不够用时,会自动向上弹出。如何分别设置向下弹出以及向上弹出的背景呢。

DataBinding PopWindow的使用方式

PopNewQcDetailBinding popBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.pop_new_qc_detail, null, false); // 提交修改 popBinding.btnCommit.setOnClickListener(new View.OnClickListener() { @Override ...

奋斗小威的专栏 1105

双目视觉入门实战:用Matlab标定相机,再到Python OpenCV完成测距的完整链路

本文详细介绍了双目视觉技术的完整实现流程,从Matlab相机标定到Python OpenCV测距的全链路实践。重点解析了Matlab与OpenCV的参数转换技巧,以及立体校正、视差计算等关键步骤的代码实现,帮助开发者快速掌握双目测距系统的搭建方法。

weixin_29166013的博客 444

K8S证书过期解决办法之替换证书

k8s证书过期解决方案之替换证书

q_hsolucky的博客 1万+

Android PopupWindow的使用

<br /> <br />这一节讲的是Android PopupWindow的使用! 在我理解其实PopupWindow其实类似于一个不能动的Widget(仅从显示效果来说!)它是浮在别的窗口之上的.下面我将给大家做一个简单的Demo,类似于音乐播放器的Widget的效果,点击Button的时候出来PopupWindow,首先我们看一下效果图:<br />下载 (9.2 KB)<br />2010-7-27 17:34<br /> <br /> <br />下载 (10.47 KB)<br />2010-7

muyu114的专栏 2687

避开这5个坑!ADNI数据库下载实操经验分享(含断点续传方案)

本文分享了ADNI数据库下载的5个常见陷阱及解决方案,包括高级搜索策略优化、断点续传技巧和数据完整性验证。特别针对大文件下载中断问题,提供了wget断点续传和分卷下载的实操方法,帮助研究者高效获取神经影像数据,避免重复下载和时间浪费。

git9versioner的博客 373

Android中PopupWindow的使用

1、Activity public class PopUpActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { supe

a623891391的专栏 1029

S7-200SMART系列全套接线说明

SMART200接线方法,输入,输出,模拟量,面板,扩展糟等,并且详细说明接线原理。

文都龙哥 1万+

FreeRTOS OTA实战:从Bootloader设计到安全升级的完整流程(附MQTT示例代码)

本文详细介绍了在FreeRTOS系统中实现安全OTA固件升级的完整流程。内容涵盖从Bootloader设计、存储分区规划到与MQTT协议集成的OTA Agent任务开发,并重点解析了固件签名验证、防回滚等关键安全机制,为嵌入式物联网设备提供了一套高可靠性的远程升级解决方案。

weixin_29248653的博客 157

android关于popupWindow不显示问题

按照API和网上的例子,制作了一个popupWindow的demo。 但是popupWindow就是不显示。 后来发现必须设置popupWindow的宽度和高度,不然是显示不出来的... 以为popupWindow绑定自定义的View就可以了,还要设置高度和宽度... setContentView(view); setWidth(LayoutParams.WRAP_CONTENT);

explorerqp的专栏 2960

【CASS精品教程】013:CASS9.1生成标准图幅案例教程

在CASS中,可以很方便的生成标准图幅,如50*50。本文演示cass中,根据测区范围标准图幅生成过程。

智慧空间实验室 1万+

popupwindow弹出窗关闭_封装使用PopupWindow弹出窗口(一)

引用现如今很多软件都有个人中心,而个人中心往往都用来设置个人信息,一般的个人中心包括:用户头像,用户名,以及其他的业务逻辑需要的集合。今天我们就来封装弹出窗口PopupWindow,点击更换头像就是很经典的例子。介绍PopupWindow用来做弹出框并不是独一份,一般系统使用的是另一种,我们很多软件的消息通知其实都是用AlertDialog,但PopupWindow好就好在它足够灵活,可以适应很多...

weixin_42238594的博客 380

Linux V3H 平台开发系列讲解(串行解串篇)2.2.2 MAX9296 MAX96705 GMSL1 续篇--串行器提前配置

本文详细介绍了在Linux v3h平台上,针对Max9296解串器和解串器max96705的详解配置,验证可出图,内容包括串行器解串器介绍、链路框架、硬件连接、速率配置,以及遇到的图像异常问题及解决方法。

xian18809311584的博客 537

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Android高手进阶教程(十)之----Android PopupWindow的使用!

Android高手进阶教程(十)之----Android PopupWindow的使用!

621

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

《Andrioid_Popupwindow案例》popWindow做Menu,popWindow做选项菜单

上图展示了最近在开发的项目经常用到的pop案例,包括一般的选项,下拉选项,menu等

简单就好 1477

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

设置popupWindow显示位置以及点击其他位置取消弹出

相对控件位置显示: 上方显示: private void showPopUp(View v) { LinearLayout layout = new LinearLayout(this); layout.setBackgroundColor(Color.GRAY); TextView tv = new TextView(this); ...

zzl8846227的博客 870

PopupWindow

布局: xml version="1.0" encoding="utf-8"?> xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_hei

Monkey_zzh的博客 307

Android popupwindow 和 Activity弹出窗口实现方式

Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于: AlertDialog的位置固定,而PopupWindow的位置可以随意 AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的 PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父

Roy的专栏 4456
上一篇: 关于Retrofit的逐渐认识(二)
下一篇: 针对下方是导航条,上方是Fragment; 包括Fragment再嵌套Fragment进行切换(类似QQ消息模块中消息,电话)的UI框架
zmybiealex
博客等级 码龄10年 48粉丝 46原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值