Android 弹出窗口上的 OnClick 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12192164/
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 09:59:41  来源:igfitidea点击:

OnClick event on Popup Window in Android

androideventspopuponclick

提问by kendrelaxman

I am working on app where I have to show Popup within my main Activity, in Pop up I have one Button on which I need to perform some operation.

我正在开发应用程序,我必须在我的主活动中显示 Popup,在 Popup 中,我有一个需要执行某些操作的按钮。

Please see my code bellow. The code does not give any error but the button click of Popup window is not working.

请看我下面的代码。该代码没有给出任何错误,但弹出窗口的按钮单击不起作用。

imgOpenPopup = (ImageView) findViewById(R.id.places);

        imgOpenPopup.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = (LayoutInflater) ConvergeActivity.this
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                View popupView = inflater.inflate(R.layout.placepopup,(ViewGroup)findViewById(R.layout.maincam));  

                popupWindow = new PopupWindow(inflater.inflate(
                        R.layout.placepopup, null, false), 200, 265, true);

                popupWindow.showAtLocation(findViewById(R.id.places),
                        Gravity.CENTER, 0, 0);


                 objbtnpopupOk=(Button)popupView.findViewById(R.id.btnokpopup); 

                objbtnpopupOk.setOnClickListener(new OnClickListener() 
                { 
                    @Override
                    public void onClick(View v) 
                    { 
                        Toast.makeText(getApplicationContext(), " hi thrtrt  " ,Toast.LENGTH_LONG).show();
                    } 
            }); 

            }
        });

回答by Pasha

Try this:

尝试这个:

View popupView = inflater.inflate(R.layout.placepopup,(ViewGroup)findViewById(R.layout.maincam));  
popupWindow = new PopupWindow(popupView, 200, 265, true);

instead this:

取而代之的是:

View popupView = inflater.inflate(R.layout.placepopup,(ViewGroup)findViewById(R.layout.maincam));  
popupWindow = new PopupWindow(inflater.inflate(R.layout.placepopup, null, false), 200, 265, true);

回答by kendrelaxman

I really dont know what went wrong but this solutionworked out for me.

我真的不知 道出了什么问题,但这个解决方案对我有用。

cheers.

干杯。

回答by eyal

I have done it this way:

我是这样做的:

 popupWindow.getContentView().setOnClickListener(new OnClickListener()...);

I hope it helps.

我希望它有帮助。

回答by Martin Rajniak

try to lift the creation of a popup window out of the OnClickListener, and in onClick just call the method to show the popup window. It has few optimization improvements and it can solve your issue.

尝试将弹出窗口的创建从 OnClickListener 中解除,并在 onClick 中调用该方法以显示弹出窗口。它几乎没有优化改进,可以解决您的问题。

回答by Akshay

Why don't you try to use AlertDialog. That will help you.Read this Alert Dialog

你为什么不尝试使用AlertDialog. 这会帮助你。阅读这个警报对话框

回答by Adam

Do the popup in another activity :

在另一个活动中执行弹出窗口:

Add this theme to the styles file and then set it to the activity that you want to pop up in the manifest file. I am sure that the button will work in another activity.

将此主题添加到样式文件中,然后将其设置为您要在清单文件中弹出的活动。我确信该按钮将在另一个活动中起作用。

<resources>
<style name="Theme.Transparent" parent="android:Theme"> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:backgroundDimEnabled">false</item> 
</style>