如何在Android应用程序启动时加载弹出窗口? onCreate()?

发布于 2024-12-29 14:48:30 字数 119 浏览 3 评论 0原文

我是android开发新手,我想问如何在android应用程序启动时加载弹出窗口? onCreate()?

我看过很多例子,但没有人满足我的需求。有没有办法在应用程序启动时加载弹出窗口?

谢谢

I am new to android development and i want to ask how to load popup window on android application startup? onCreate()?

I have seen many examples but no one covered my needs. Is there a way to load a popup window on application start?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2025-01-05 14:48:30

执行此操作的最佳位置是在 Activity 的 onStart 方法中。本质上,我们需要:

  • 获取一个新的 Dialog,并指定所需的 XML 布局。
  • 填写任何其他选项,包括当用户单击对话框上的按钮时运行的代码(在本例中,只需关闭对话框)。
  • 显示对话框。

这是一个简单的示例,但有很多可用的选项。有关详细信息,请参阅 http://developer.android.com/reference/android/应用程序/Dialog.html

res/layout/dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
  <TextView android:text="hello, world" 
            android:id="@+id/TextView01"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/>
   <Button android:id="@+id/Button01" 
           android:layout_below="@id/TextView01"
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content"
           android:layout_centerHorizontal="true" 
           android:text="OK" />
</RelativeLayout>

在您的活动中

@Override
protected void onStart()
{
    super.onStart();

    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog);
    dialog.setTitle("Dialog box");

    Button button = (Button) dialog.findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() {  
        @Override  
        public void onClick(View view) {  
            dialog.dismiss();            
        }  
    });

    dialog.show();
}

The best place to do this is in the onStart method of your Activity. Essentially, we need to:

  • Get a new Dialog, and specify the XML layout you want.
  • Fill in any other options, including code to run when the user clicks on the buttons on the dialog (in this case, simply closing the dialog).
  • Show the dialog.

Here's a simple example, but there are lots of options avaiable. For more information, see http://developer.android.com/reference/android/app/Dialog.html.

res/layout/dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
  <TextView android:text="hello, world" 
            android:id="@+id/TextView01"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/>
   <Button android:id="@+id/Button01" 
           android:layout_below="@id/TextView01"
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content"
           android:layout_centerHorizontal="true" 
           android:text="OK" />
</RelativeLayout>

In your activity

@Override
protected void onStart()
{
    super.onStart();

    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog);
    dialog.setTitle("Dialog box");

    Button button = (Button) dialog.findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() {  
        @Override  
        public void onClick(View view) {  
            dialog.dismiss();            
        }  
    });

    dialog.show();
}
鹿港巷口少年归 2025-01-05 14:48:30

您想要创建一个新活动并通过 Intent 启动它。基本上,如果您希望它“感觉”像弹出窗口,您只需创建一个不占用 100% 屏幕宽度/高度的布局即可。

执行此操作的方法如下:

  1. 创建一个 XML 布局并将其放置在 res/layout/PopupActivity.xml 中
  2. 创建一个新活动 PopupActivity.java。
  3. 将新活动添加到您的 Android 清单文件中。
  4. 通过主活动的 onResume 方法中的 Intent 启动活动: startActivity(new Intent(this, PopupActivity.class));

如果您想在用户完成弹出活动时收到通知,您可以使用 startActivityForResult() 而不仅仅是 startActivity() 来启动它。这允许您在用户完成活动时收到回调。

我建议使用 onResume() 来启动,因为当用户返回到您的 Activity 时(无论是第一次还是随后的多次),它总是会被调用。

您将需要保留一个代表应用程序状态的值,以便决定何时/是否显示 PopupActivity。 Android 会根据需要定期杀死您的应用程序,因此,如果您没有实现某种持久性,那么当用户访问该应用程序时,您的 PopupActivity 将始终出现。我将开始在您的主要活动中实现 onSaveInstanceState() 和 onRestoreInstanceState() 。

知道是否显示 PopupActivity 的一个简单方法是使用一个变量,例如 popupShown 默认情况下将其设置为 false,在显示弹出窗口后将其设置为 true。在启动弹出窗口之前,检查 popupShown 是否为 true。正如我上面提到的,保存应用程序的状态将确保您仅在需要时显示弹出窗口。

了解 Android 如何管理 Activity 的生命周期非常重要,否则您将浪费大量时间来思考为什么应用程序行为异常。我的第一个 Android 应用程序非常无法使用,因为我只是一头扎进去,并没有完全理解生命周期。

Android Lifecycle

如果您不完全确定生命周期,您应该仔细阅读此内容:http://developer.android.com/reference/android/app/Activity.html

You want to create a new activity and launch it via an Intent. Basically, if you want it to 'feel' like a popup, you just create a layout that does not take 100% of the screen's width/height.

Here is how you can do that:

  1. Create an XML layout and place it in res/layout/PopupActivity.xml
  2. Create a new activity, PopupActivity.java.
  3. Add the new activity to your Android manifest file.
  4. Launch the activity via an Intent from the onResume method of your main activity: startActivity(new Intent(this, PopupActivity.class));

If you want to receive notification when the user finished with your popup activity, you can launch it using startActivityForResult() instead of just startActivity(). This allows you receive a callback when the user has finished with the activity.

I recommend using onResume() to launch because it is always called when the user returns to your activity, either the first time, or subsequent times.

You will need persist a value representing the state of your application in order to decide when/if you will display your PopupActivity. Android will periodically kill off your application as needed, therefore, if you do not implement some sort of persistence, then your PopupActivity will always appear when the user comes to the application. I would start be implementing onSaveInstanceState() and onRestoreInstanceState() in your main activity.

A simple way to know whether to show the PopupActivity or not, is to just have a variable, such as popupShown set it false by default, and true after the popup has been show. Before launching the popup, check if popupShown is true. Saving the state of the application, as I mentioned above, will ensure that you only show the popup when needed.

It's very important to understand how Android manages the lifecycle of activities, otherwise you will be wasting a lot of time wondering why your application is behaving oddly. My first Android application was very much unusable because I just dove in and didn't fully understand the lifecycle.

Android Lifecycle

You should read through this if you aren't completely sure on the lifecycle bit: http://developer.android.com/reference/android/app/Activity.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文