如何将Dialog与PopupWindow一起使用?

时间:2019-04-02 07:12:28

标签: android dialog popupwindow

当您在PopupWindow中创建一个Button并单击该按钮时,将显示对话框。我希望Dialog从后台的任何地方消失。

setCanceledOnTouchOutside(true); setCancelable(true);

我尝试使用它,但是没有用。

正是我想要做的。 如果上面在PopupWindow上创建的对话框单击了背景或单击了“对话框窗口”以外的其他位置,则该对话框被关闭。 我需要做什么方面的帮助。

或者除此以外,还有其他方法可以制作此形状吗? 这是一张奇怪的图片,但已作为参考上传。

enter image description here

MainActivity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    boolean res = false;

    switch(keyCode)
    {
        case KeyEvent.KEYCODE_1 :
            res = procKeyUP(keyCode, event);
            break;
        case KeyEvent.KEYCODE_5 :
            res = procKeyAdjust(keyCode, event);
            break;
    }
    return true;
}
private boolean procKeyAdjust(int key, KeyEvent event)
{
    boolean res = false;

    LayoutInflater mProcKeyAdjust = (LayoutInflater)instance.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mProcAdjustPopup = mProcKeyAdjust.inflate(R.layout.popup_menu, null);
    mProcAdjustPopup.setBackgroundColor(Color.GRAY);
    mProcAdjustPopup.getBackground().setAlpha(220);

    _Btn1_ex = mProcAdjustPopup.findViewById(R.id.layer_popup_btn_2_2_1);
    _Btn2_ex = mProcAdjustPopup.findViewById(R.id.layer_popup_btn_1_2_2_1);

    _Btn1_ex.setOnClickListener(this);
    _Btn2_ex.setOnClickListener(this);

    AdjustPopupMenuEvent();
    AdjustPopupEvent();

    return true;
}


public void onClick(View v) {
    switch (v.getId()) {
        case R.id.layer_popup_btn_2_2_1 :
            //startActivity(new Intent(this, ModeDialog.class));
            break;

        case R.id.layer_popup_btn_1_2_2_1 :
            RangeDialog mRangeDialog = new RangeDialog(this);
            mRangeDialog.show();
            break;

        default:
            break;
    }
}

RangeDialog

public static Button
        _Btn2;
public static RangeDialog instan;
public Context context;

public RangeDialog(Context context) {
    super(context);
    this.context = context;
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.popup_range_menu);

    instan = this;

    this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    this.setCanceledOnTouchOutside(true);
    this.setCancelable(true);

    WindowManager.LayoutParams params = this.getWindow().getAttributes();
    params.x = 50;
    params.y = -45;

    this.getWindow().setAttributes(params);

    _Btn2 = (Button) findViewById(R.id.button8);

    ButtonAction();
}


@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    Rect dialogBounds = new Rect();
    getWindow().getDecorView().getHitRect(dialogBounds);

    if (!dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
        this.dismiss();
    }
    return super.dispatchTouchEvent(ev);
}


public static void ButtonAction()
{
    _Btn2.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View view) {
            instan.dismiss();
        }
    });
}

1 个答案:

答案 0 :(得分:0)

查看此内容并根据您的需要对其进行自定义。

public class ShowPopUp extends Activity {

     PopupWindow popUp;
     LinearLayout layout;
     TextView tv;
     LayoutParams params;
     LinearLayout mainLayout;
     Button but;
     boolean click = true;

     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      popUp = new PopupWindow(this);
      layout = new LinearLayout(this);
      mainLayout = new LinearLayout(this);
      tv = new TextView(this);
      but = new Button(this);
      but.setText("Click Me");
      but.setOnClickListener(new OnClickListener() {

       public void onClick(View v) {
        if (click) {
         popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
         popUp.update(50, 50, 300, 80);
         click = false;
        } else {
         popUp.dismiss();
         click = true;
        }
       }

      });
      params = new LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
      layout.setOrientation(LinearLayout.VERTICAL);
      tv.setText("Hi this is a sample text for popup window");
      layout.addView(tv, params);
      popUp.setContentView(layout);
      // popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
      mainLayout.addView(but, params);
      setContentView(mainLayout);
     }
    }

希望这可以解决您的问题。