如何在PopupWindow中使用Dialog?

问题描述 投票:0回答:1

在PopupWindow中创建Button并单击该按钮时,将出现对话框。我希望Dialog能够从后台的任何地方消失。

setCanceledOnTouchOutside(true); setCancelable(true);

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

正是我想要做的。如果上面在PopupWindow上创建的对话框单击背景或单击对话窗口以外的任何位置,则对话框将被解除。我需要帮助做什么。

或者有没有办法使这个形状除此之外?这是一张奇怪的图片,但它作为参考上传。

主要信息

@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();
        }
    });
}
android dialog popupwindow
1个回答
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);
     }
    }

希望这能解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.