简体   繁体   English

Android 弹出窗口与 Android 对话框

[英]Android Popup Window vs Android Dialog

I am trying to implement a simple logic in my application where the user is shown a popup (after sometime of application launch).我试图在我的应用程序中实现一个简单的逻辑,其中向用户显示一个弹出窗口(在应用程序启动一段时间后)。 The popup simply shows a TextView with some info message.弹出窗口只是显示一个带有一些信息消息的TextView This message is refreshed every time the application is launched and a new message is shown.每次启动应用程序时都会刷新此消息并显示一条新消息。 The UI of the popup matches my application UI - here maybe just popup background images is needed.弹出窗口的 UI 与我的应用程序 UI 匹配 - 这里可能只需要弹出背景图像。 Also one close button (X) is shown at the top right corner of the popup - to close this popup.还有一个关闭按钮 (X) 显示在弹出窗口的右上角 - 关闭此弹出窗口。

Logic of Showing Message: I have a String array having some 100 strings stored in it.显示消息的逻辑:我有一个String数组,其中存储了大约 100 个字符串。 I randomly pick one string from this array and populate the popup TextView showing the message.我从这个数组中随机选择一个字符串并填充显示消息的弹出式TextView Please suggest if there is any better approach than what I am doing already here.请建议是否有比我已经在这里做的更好的方法。 Also is it possible to logic that if one message is picked then the same message is not picked until the other messages are shown at least once?是否也有可能逻辑,如果选择了一条消息,那么在其他消息至少显示一次之前不会选择相同的消息?

Logic of Showing Popup: This is what I am not able to implement.显示弹出窗口的逻辑:这是我无法实现的。 I do not want to anchor the popup with any user Event or Button click.我不想通过任何用户EventButton单击来锚定弹出窗口。 I simply wants to show the message after some time - say我只是想在一段时间后显示消息 - 说

Thread.sleep(3000);线程睡眠(3000);

Now I have tried to use PopupWindow for this using the below code.现在我尝试使用以下代码使用PopupWindow

PopupWindow infoPopup;    
LinearLayout infoLayout;
TextView infoTextView;
Button infoButton;
infoTextView = new TextView(this);
infoTextView.setText("Testing Popup Text");
infoTextView.setPadding(10,10,10,10);
infoButton = new Button(this);
infoButton.setText("Close");    
infoLayout = new LinearLayout(this);
infoLayout.setOrientation(LinearLayout.VERTICAL);
infoLayout.addView(infoTextView);
infoLayout.addView(infoButton);    
infoPopup = new PopupWindow(infoLayout,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
infoPopup.setContentView(infoLayout);    
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}    
infoPopup.showAtLocation((CoordinatorLayout)findViewById(R.id.main_content),Gravity.CENTER,100,100);

But this popup is showing error at the last line giving null pointer on my但是这个弹出窗口在最后一行显示错误,在我的

(CoordinatorLayout)findViewById(R.id.main_content) (CoordinatorLayout)findViewById(R.id.main_content)

parameter.参数。

The issue that I am getting are:我得到的问题是:

  1. First of all, I am not sure if this is the right approach of showing a custom UI popup.首先,我不确定这是否是显示自定义 UI 弹出窗口的正确方法。 I am aware of AlertDialog but not sure which is the best option to go in this case - Please suggest.我知道AlertDialog但不确定在这种情况下哪个是最好的选择 - 请建议。

  2. Why the CoordinatorLayout is showing null pointer?为什么CoordinatorLayout显示空指针?

  3. How to implement the top right (X) button logic in this Popup ?如何在此 Popup 中实现右上角 (X) 按钮逻辑?

1. Yes there are so many options for showing a custom UI popup in Android. 1.是的,有很多选项可以在 Android 中显示自定义 UI 弹出窗口。 You might select one from PopupWindow , AlertDialog or Dialog Activity .您可以从PopupWindowAlertDialog或 Dialog Activity选择一个。 You need to decide which suits you best.您需要决定哪种方式最适合您。

If you need to customize your UI a lot and have to show a list or some complex GUI then I would suggest you launch an Activity with theme.Dialog .如果您需要大量自定义 UI 并且必须显示列表或一些复杂的 GUI,那么我建议您使用theme.Dialog启动一个Activity Just set the theme of the Activity to something like this android:theme="@android:style/Theme.Holo.Light.Dialog" .只需将Activity的主题设置为android:theme="@android:style/Theme.Holo.Light.Dialog" There's a plenty of tutorials for implementing a dialog Activity .有很多教程用于实现对话框Activity

PopupWindow is another tool to customize your custom pop up anywhere in the screen. PopupWindow是另一种工具,可以在屏幕的任何位置自定义您的自定义弹出窗口。 If you're showing this popup always in the middle of the screen, then I would like to suggest not to use this.如果你总是在屏幕中间显示这个弹出窗口,那么我建议不要使用它。 The AlertDialog should work fine. AlertDialog应该可以正常工作。

AlertDialog has many variants and as far as I can assume your problem, this one suits you best. AlertDialog有很多变体,就我可以假设您的问题而言,这个最适合您。 You can have a cross button too in the top-right corner of the dialog (You can set the icons anywhere, as you can provide a custom layout to an AlertDialog ).您也可以在对话框的右上角有一个十字按钮(您可以在任何地方设置图标,因为您可以为AlertDialog提供自定义布局)。 Personally I use this library to provide a custom layout to my AlertDialog .我个人使用这个为我的AlertDialog提供自定义布局。 You can have a look at this too.你也可以看看这个。

2. The NullPointerException is simple. 2. NullPointerException很简单。 Your layout doesn't have any id named main_content .您的布局没有任何名为main_content ID。 Post your logcat if this doesn't solve your problem.如果这不能解决您的问题,请发布您的 logcat。 Post the layout too.也贴一下布局。

3. As I've told you earlier, I use the library to provide a custom layout to an AlertDialog and you can have a look at it too. 3.正如我之前告诉您的,我使用该库为AlertDialog提供自定义布局,您也可以查看它。 So after implementing this library you can easily design your own layout with a cross button and implement the onClick functionalities easily.因此,在实现这个库之后,您可以轻松地使用十字按钮设计自己的布局并轻松实现onClick功能。

Hope this helps.希望这会有所帮助。

  1. Activity with theme Dialog.带有主题对话框的活动。
    This is not a good idea.这不是一个好主意。 It looks like a pop-up, but you can't click outside the pop-up to close it.它看起来像一个弹出窗口,但您无法在弹出窗口外部单击以关闭它。

  2. PopupWindow弹出窗口
    It will stop the application.它将停止应用程序。 When the user finish clicking the pop-up, the application can work again.当用户点击弹出窗口后,应用程序可以再次运行。

  3. AlertDialog警报对话框
    The best one, it will not stop the application and can be closed by clicking outside the dialog.最好的一个,它不会停止应用程序,并且可以通过在对话框外单击来关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM