【问题标题】:Android PopupWindow button disappears after clickAndroid PopupWindow 按钮点击后消失
【发布时间】:2012-06-12 08:46:41
【问题描述】:

我在使用 Android 上的 PopupWindow 时遇到了一个非常令人沮丧的问题。 我已经实现了自己的类,它继承了 PopupWindow 并实现了 OnClickListener

使用自定义选择器添加按钮背景后,问题开始。 单击按钮(启动新活动并关闭弹出窗口)后,此背景不断消失。 在“focus and click”之后它不会消失,只有在“quick click”之后才会消失。

任何想法/建议都会非常受欢迎!

public class TestPopup extends PopupWindow implements OnClickListener

protected LayoutInflater inflater;
protected Activity caller;
protected View popup;
protected View layout;

public TestPopup(Activity activity) {
    super(activity);
    popup = inflater.inflate(R.layout.popup, (ViewGroup) caller.findViewById(R.id.contentLayout));
    layout = popup.findViewById(R.id.layout);

    popup.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

    Display display = caller.getWindowManager().getDefaultDisplay();
    setHeight(display.getHeight());
    setWidth(display.getWidth());
    setFocusable(true);
    setContentView(popup);

    // fix to allow Popup to be clickable!
    setBackgroundDrawable(new BitmapDrawable());

    popup.setOnClickListener(this);
    popup.findViewById(R.id.addButton).setOnClickListener(this);
    popup.findViewById(R.id.deleteButton).setOnClickListener(this);
}

public void onClick(View v) {
    Intent intent = null;
    if (v.getId() == R.id.addButton) {
        intent = new Intent(caller, AddActivity.class);
        intent.putExtra(AddActivity.ACTION_ADD, true);
    } else if (v.getId() == R.id.deleteButton) {
        intent = new Intent(caller, AddActivity.class);
        intent.putExtra(AddActivity.ACTION_DELETE, true);
    }

    if (intent != null) {
        caller.startActivity(intent);
    }

    TestPopup.this.dismiss();
}

【问题讨论】:

    标签: android popupwindow onclicklistener


    【解决方案1】:

    一种解决方案是在关闭弹出窗口之前调用 popup.invalidate();

    【讨论】:

    • 谢谢!使用您的答案解决类似的问题。
    • 这也解决了我的类似问题,但是删除了我需要的动画... :(
    猜你喜欢
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多