Android如何创建弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23028480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:41:42  来源:igfitidea点击:

Android how to create popup window

androidbuttonpopupwindow

提问by user3132352

I need to create a popup window with buttons and a button that will close the popup. I found some tutorials but I couldn't find out how to do the implementation.

我需要创建一个带有按钮的弹出窗口和一个关闭弹出窗口的按钮。我找到了一些教程,但我不知道如何执行。

What I want to do: Click an action button and the popup shows and when I click the the close button the popup window must close.

我想要做什么:单击操作按钮并显示弹出窗口,当我单击关闭按钮时,弹出窗口必须关闭。

There were an onCreate method in the tuorials and I didn't understand how is it called.

教程中有一个 onCreate 方法,我不明白它是如何调用的。

Can somebody give an example of an popup implementation or a link to a good tutorial? Thank you!

有人可以举一个弹出实现的例子或一个好的教程的链接吗?谢谢!

回答by Zeeshan Khan

private void showPopup(){
    Button btn_closepopup=(Button)layout.findViewById(R.id.btn_closePoppup);
    pwindo=new PopupWindow(layout,480,500,true);
    pwindo.showAtLocation(layout, Gravity.CENTER, 0, 40);
    chartContainer1.addView(mChart);
    btn_closepopup.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            pwindo.dismiss();
        }
    });
}

回答by Dhina k

private void callPopup() {

    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
        .getSystemService(LAYOUT_INFLATER_SERVICE);

    View popupView = layoutInflater.inflate(R.layout.popup, null);

    popupWindow=new PopupWindow(popupView,
        LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT,        
        true);

    popupWindow.setTouchable(true);
    popupWindow.setFocusable(true);

    popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
    Name = (EditText) popupView.findViewById(R.id.edtimageName);

    ((Button) popupView.findViewById(R.id.saveBtn))
        .setOnClickListener(new OnClickListener() {

        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        public void onClick(View arg0) {
            Toast.makeText(getApplicationContext(),
                Name.getText().toString(),Toast.LENGTH_LONG).show();

            popupWindow.dismiss();
        }
    });

    ((Button) popupView.findViewById(R.id.cancelbtutton))
        .setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            popupWindow.dismiss();
        }
    });
}