点击后Android PopupWindow按钮消失

标签 android popupwindow onclicklistener

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

使用自定义选择器添加按钮背景后,问题开始出现。 单击按钮(开始新 Activity 并关闭弹出窗口)后,此背景不断消失。 它不会在“聚焦并点击”后消失,只会在“快速点击”后消失。

如有任何想法/建议,我们将不胜感激!

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();
}

Popup

最佳答案

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

关于点击后Android PopupWindow按钮消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10993211/

相关文章:

java - CircleButton LongClickListener 未触发

java - 如何让弹窗背景模糊

android - ViewPager with Fragments inside PopupWindow(或 DialogFragment) - 错误找不到 fragment id 的 View

javascript - 在另一个点击事件之后添加点击事件

Android ViewModel 和点击监听器

java - onClick Textview 将另一个字符串传递给 intent

android - 动态地将图片添加到图库小部件

android - 使用 viewpager 和 radiogroup 在 fragment 之间切换

Java - 在一秒内多次调用包含 for 循环的方法会导致主线程的阻塞最小化

javascript - 如何使用 `window.open` 显示模态弹出窗口?