FLAG_NOT_FOCUSABLE和FLAG_NOT_TOUCH_MODAL

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
如果没有设置FLAG_NOT_FOCUSABLE,那么屏幕上弹窗之外的地方不能点击。如果设置了FLAG_NOT_FOCUSABLE,那么屏幕上弹窗之外的地方能够点击、但是弹窗上的EditText无法输入、键盘也不会弹出来。

WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
如果设置了FLAG_NOT_TOUCH_MODAL,那么屏幕上弹窗之外的地方能够点击、弹窗上的EditText也可以输入、键盘能够弹出来。

package com.linux.collect.view;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.linux.bean.YourData;//这里可以放你自定义的数据

public class PopWindow extends RelativeLayout {
    private final String hint="Please input the OTP";
    private Button myButton;
    private EditText myEditText;
    //https://www.jianshu.com/p/7aedea560f16

    public PopWindow(final Context context, final YourData response){
        super(context);

        myButton = new Button(context);
        myButton.setText("Press me");
        myButton.setBackgroundColor(Color.RED);

        myEditText = new EditText(context);
        myEditText.setHint(hint);

        //设置Button的布局参数
        RelativeLayout.LayoutParams buttonParams =
                new RelativeLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT);
        buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
        buttonParams.setMargins(80, 0, 80, 20);//这里的80是px


        //设置EditText的布局参数
        RelativeLayout.LayoutParams textParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);

        textParams.addRule(RelativeLayout.ABOVE, myButton.getId());
        textParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        textParams.setMargins(80, 80, 80, 20);//这里的80是px

//        //设置EditText的宽度为指定大小宽度,要相应的dp转化为px
//        Resources r = getResources();
//        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, r.getDisplayMetrics());
//        myEditText.setWidth(px);
        //将布局添加到父容器中
        PopWindow.this.addView(myButton, buttonParams);
        PopWindow.this.addView(myEditText, textParams);
        PopWindow.this.setBackgroundColor(Color.parseColor("#88FFFFFF"));//设置为半透明状态

        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String code= myEditText.getText().toString();
               if(code==null||code.length()==0){
                   Toast.makeText(context,hint,Toast.LENGTH_SHORT).show();
               }else {
                   Toast.makeText(context,code,Toast.LENGTH_SHORT).show();
                   if(PopWindow.this.isShown()) {
                       PopWindow.this.removeAllViews();
                       WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                       manager.removeView(PopWindow.this);
                   }
               }

            }
        });

    }


    public static void pop(final Context context, final YourData response){

        try {
            /设置LayoutParams参数
            WindowManager.LayoutParams params = new WindowManager.LayoutParams();
            params.format = PixelFormat.RGBA_8888;
//            params.flags|= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;//没有这句的话,手机会出现一个奇怪的现象。除了点击弹窗,屏幕上的其他东西都点不了,用户看到的手机就像卡死了一样。如果弹窗没有正常显示,用户就感觉手机莫名其妙的卡,卡爆了。但是EditText不能输入

            params.flags|= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;//https://blog.csdn.net/iromkoear/article/details/68936832 屏幕其他部分可以点击&&EditText也可以输入

            //根据手机屏幕尺寸动态设置弹窗的尺寸
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(dm);
            double screenWidth = dm.widthPixels; // 屏幕宽度(像素)
            double screenHeight = dm.heightPixels;// 屏幕高度(像素)
            params.width =(int)(screenWidth*3/4);
            params.height =(int)(screenHeight/3);
            params.gravity = Gravity.CENTER;

            //判断有无Alert_Window权限的逻辑
            PackageManager pm = context.getPackageManager();
            boolean permission = false;
            try {
                permission = (PackageManager.PERMISSION_GRANTED ==pm.checkPermission("android.permission.SYSTEM_ALERT_WINDOW", context.getPackageName()));
            } catch (Exception e) {
                // TODO: handle exception
            }
            catch (Error e) {	// 运行异常无法判断的情况,那就直接获取了
                // TODO: handle exception
            }
            if (!permission) {
                params.type = WindowManager.LayoutParams.TYPE_TOAST;
                if(android.os.Build.VERSION.SDK_INT <= 18){
                    params.width = 0;
                    params.height = 0;
                }
            }else {
                if(android.os.Build.VERSION.SDK_INT <= 18){
                    params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
                }else {
                    params.type = WindowManager.LayoutParams.TYPE_TOAST;
                }
            }

            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            PopWindow view=new PopWindow(context, response);
            manager.addView(view, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Android,view)