PopUp窗口不会在后退按钮上关闭

时间:2014-06-16 18:13:24

标签: android popupwindow dismiss

我正在努力弄清楚为什么弹出不会消失。 我在互联网上阅读了很多答案,但没有任何效果。

这是我的代码:

初​​始化:

LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.pop_up,
(ViewGroup) findViewById(R.id.popup_element));
mPopUp = new PopupWindow(layout, mScreenWidth, mScreenHeight, true);
mPopUp.showAtLocation(layout, Gravity.CENTER, 0, 0);
mPopUp.setBackgroundDrawable(new ShapeDrawable());

背面按下:

public void onBackPressed() {
if(mPopUp!=null){
mPopUp.dismiss();
}
else{
super.onBackPressed();
}
}

我真的不知道该怎么办。
我读到了应该放mPopUp.setBackgroundDrawable(new ShapeDrawable());的地方 初始化后但没有运气。我已经尝试了一切。

提前谢谢。

编辑: 在我的日志中,我可以收到错误:

Access to extended visibility flags denied: Requires com.sonymobile.permission.SYSTEM_UI_VISIBILITY_EXTENSIONS permission.

1 个答案:

答案 0 :(得分:1)

我喜欢使用DialogFragment,因为它允许更多的自定义。

public class Popup extends DialogFragment implements View.OnClickListener {
    Context c;

    public static Popup newInstance() {
        Popup f = new Popup ();

        // Supply num input as an argument.
        Bundle args = new Bundle();

        f.setArguments(args);

        return f;
    }

    public void params(Context c){
        this.c = c;
    }




    @Override
    public void onCreate(Bundle sis){
        super.onCreate(sis);

        int style, theme;

        style = DialogFragment.STYLE_NO_FRAME;
        theme = android.R.style.Theme_Holo_Dialog;


        setStyle(style, theme);

    }





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.stats, container, false);
        this.v = v;
        setButtons();
        return v;
    }



    private void setButtons(){
        //set up all buttons, textviews etc
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                dismiss();
                break;


        }

    }
}

调用整个事情:

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment prev = getFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    PopupnewFragment = Popup.newInstance();
    newFragment.params(getBaseContext(), clicker, this);
    newFragment.show(ft, "dialog");

现在,对于您收到的错误:

索尼是一个非常奇怪的制造商。他们创建了自己的权限,而不关心基本系统权限允许的内容。需要SOny的UI可见性权限,因为您可能正在使用索尼手机进行测试。

为什么索尼有自己的权限?

因为每个不是nexus的手机都是Android的修改版。每个制造商都会创建自己稍微修改过的操作系统版本,这样他们就可以将自己的应用程序放入手机中,而无需用户安装。

因此,如果您想要解决错误,则必须添加该权限,以便Sony手机可以使用您的应用。另外,你可以阻止索尼对应用程序的访问权限,或者你可以尝试另一种方式,如上所述

相关问题