Android弹出窗口与Android对话框 [英] Android Popup Window vs Android Dialog

查看:1256
本文介绍了Android弹出窗口与Android对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中实现一个简单的逻辑,其中向用户显示一个弹出窗口(在应用程序启动后的某个时间).弹出窗口仅显示带有某些信息的TextView.每次启动应用程序并显示一条新消息时,都会刷新此消息. 弹出窗口的UI与我的应用程序UI匹配-在这里也许只需要弹出背景图像.弹出窗口的右上角还显示了一个关闭按钮(X),以关闭该弹出窗口.

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. 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. Also one close button (X) is shown at the top right corner of the popup - to close this popup.

显示消息的逻辑:我有一个String数组,其中存储了约100个字符串.我从该数组中随机选择一个字符串,并填充显示消息的弹出窗口TextView.请提出是否有比我现在在这里做的更好的方法.同样有逻辑的是,如果一条消息被选中,那么直到另一条消息至少显示一次才选择同一条消息?

Logic of Showing Message: I have a String array having some 100 strings stored in it. I randomly pick one string from this array and populate the popup TextView showing the message. 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?

显示弹出窗口的逻辑:这是我无法实现的.我不想使用任何用户EventButton单击来锚定弹出窗口.我只是想在一段时间后显示消息-说

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. I simply wants to show the message after some time - say

Thread.sleep(3000);

Thread.sleep(3000);

现在,我已尝试通过以下代码使用PopupWindow.

Now I have tried to use PopupWindow for this using the below code.

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)

参数.

我遇到的问题是:

  1. 首先,我不确定这是否是显示自定义UI弹出窗口的正确方法.我知道AlertDialog,但不确定在这种情况下哪个是最佳选择-请提出建议.

  1. First of all, I am not sure if this is the right approach of showing a custom UI popup. I am aware of AlertDialog but not sure which is the best option to go in this case - Please suggest.

为什么CoordinatorLayout显示空指针?

如何在此Popup中实现右上(X)按钮逻辑?

How to implement the top right (X) button logic in this Popup ?

推荐答案

1..是的,有很多选项可以在Android中显示自定义UI弹出窗口.您可以从PopupWindowAlertDialog或对话框Activity中选择一个.您需要确定最适合您的.

1. Yes there are so many options for showing a custom UI popup in Android. You might select one from PopupWindow, AlertDialog or Dialog Activity. You need to decide which suits you best.

如果您需要大量自定义UI并必须显示列表或一些复杂的GUI,那么我建议您使用theme.Dialog启动Activity.只需将Activity的主题设置为类似android:theme="@android:style/Theme.Holo.Light.Dialog"的内容.有很多关于实现对话框Activity的教程.

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. Just set the theme of the Activity to something like this android:theme="@android:style/Theme.Holo.Light.Dialog". There's a plenty of tutorials for implementing a dialog Activity.

PopupWindow是另一个可自定义屏幕上任意位置的自定义弹出窗口的工具.如果您总是在屏幕中间显示此弹出窗口,那么我建议您不要使用此弹出窗口. AlertDialog应该可以正常工作.

PopupWindow is another tool to customize your custom pop up anywhere in the screen. 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提供自定义布局).我个人使用此为我的AlertDialog提供自定义布局.您也可以看一下.

AlertDialog has many variants and as far as I can assume your problem, this one suits you best. 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). Personally I use this library to provide a custom layout to my AlertDialog. You can have a look at this too.

2..NullPointerException很简单.您的布局没有任何名为main_content的ID.如果这样不能解决您的问题,请发布您的logcat.也发布布局.

2. The NullPointerException is simple. Your layout doesn't have any id named main_content. Post your logcat if this doesn't solve your problem. Post the layout too.

3..如前所述,我使用该库为AlertDialog提供自定义布局,您也可以查看它.因此,在实现此库之后,您可以轻松地使用十字按钮设计自己的布局,并轻松实现onClick功能.

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. So after implementing this library you can easily design your own layout with a cross button and implement the onClick functionalities easily.

希望这会有所帮助.

这篇关于Android弹出窗口与Android对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆