AndroidAndroid开发Android开发经验谈

自定义弹窗

2019-12-05  本文已影响0人  瑟闻风倾

(1) PauseDialog.java

package com.jack.workticket.dialog;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import com.jack.workticket.R;
import com.jack.workticket.persist.Settings;

/**
* Created by liy on 2016-12-07.
*/
public class PauseDialog extends PopupWindow {
  private Activity activity;
  private View view;
  private Button btnContinue;
  private Settings settings;

  public PauseDialog(Activity activity){
    super(activity);
    this.activity=activity;
    //获得弹出窗口布局
    view=View.inflate(activity, R.layout.dialog_work_pause,null);
    setContentView(view);    //设置SelectPicPopupWindow的View
    //对弹出窗口进行设置
    WindowManager windowManager=activity.getWindowManager();
    setWidth(windowManager.getDefaultDisplay().getWidth()/2-40);    //设置SelectPicPopupWindow弹出窗体的宽
    setHeight(windowManager.getDefaultDisplay().getHeight()*2/3);    //设置SelectPicPopupWindow弹出窗体的高
    //*即使获取焦点,但如果不设置PopupWindow的背景,那么无论是点击外部区域还是Back键都无法dismiss弹框
    ColorDrawable colorDrawable=new ColorDrawable(0x70000000);    //实例化一个ColorDrawable颜色为半透明
    setBackgroundDrawable(colorDrawable);    //设置SelectPicPopupWindow弹出窗体的背景
    //设置窗口弹出时activity的显示效果
    WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
    layoutParams.alpha = 0.5f;    //调整activity的透明度
    activity.getWindow().setAttributes(layoutParams);
    //默认打开PupupWindow显示无焦点、框口外点击无效窗口不消失、窗口内可点击
    setFocusable(false);    //设置为false时说明即使设置了背景不为空也不能点击外面消失,只能由dismiss()消失;但是外面的View的事件还是可以触发,(back键也可以顺利dismiss掉?)
    setOutsideTouchable(false);    //设置显示PopuWindow之后在外面区域点击弹窗是否消失
    setTouchable(true);    //设置显示PopuWindow之后在弹窗内点击是否有效

    initView();
    initListener();
  }

  private void initView(){
    settings=Settings.getInstance(activity);
    btnContinue = (Button) view.findViewById(R.id.pause_continue);
  }

  private void initListener(){
    //设置弹出窗口消失的时窗体背景变亮
    setOnDismissListener(new OnDismissListener() {
      @Override
      public void onDismiss() {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 1.0f;
        activity.getWindow().setAttributes(lp);
      }
    });

    btnContinue.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        settings.setPauseFlag(false);
        dismiss();
        //具体逻辑:暂停计时器停止计时,工作计时器直接恢复计时还是需要再点击开始来恢复计时
      }
    });
  }

  public void showBy(View parent){
    showAtLocation(parent, Gravity.CENTER,0,0);
  }

}

(2) activity中调用

PauseDialog pauseDialog =new PauseDialog (activity);
pauseDialog .showBy(activity.getWindow().getDecorView());

拓展:系统弹框的使用

AlertDialog.Builder ab = new AlertDialog.Builder(getApplicationContext())
         .setTitle("自动连接上次设备失败")
         .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialogInterface, int i) {
                                         
                  }
         });
AlertDialog alertDialog = ab.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
上一篇 下一篇

猜你喜欢

热点阅读