从 main.xml 布局切换到另一个布局

发布于 2024-11-06 09:19:15 字数 687 浏览 1 评论 0原文

我在这里有一个基本的 Android 问题:

我有一个 main.xml 布局,该布局在应用程序启动时加载。此页面有一个菜单按钮,我想(单击时)将用户发送到另一个布局(about.xml)。

我怀疑这是正确的。单击后,此命令将启动:

setContentView(R.layout.about);

它似乎有效,我确实看到了 about.xml 页面,但当我点击 Android 设备上的“后退”按钮时,我无法导航回 main.xml 布局,应用程序只是关闭。

我怀疑这是在 xml 布局文件之间导航的正确方法。您能否帮助我或向我指出一个页面,为像我这样的计算机程序员初学者阐明这一点?

非常感谢你,

帕特

编辑: 感谢您提供的所有答案,为我指明了正确的方向。为了帮助像我这样的未来菜鸟程序员理解活动,这里有一个我在网上找到的一个很棒的简单教程,为我们初学者绘制了它!

http://www.warriorpoint。 com/blog/2009/05/24/android-how-to-switch- Between-activities/

I have a basic Android question here:

I have a main.xml layout that loads up when the app is launched. This page has a menu button that I'd like to (when clicked) send the user to another layout (about.xml).

I doubt this is the right. When clicked this command is kicked in:

setContentView(R.layout.about);

And it seems to work, I do see the about.xml page, but I cannot navigate back to the main.xml layout when I hit the BACK button on my Android device, the app just closes.

I doubt that this is the right way to navigate between xml layout files. Can you please help or point me to a page that spells this out for a computer programmer beginner like myself?

Thank you very much,

Pat

EDIT:
Thanks for all the answers you helped point me in the right direction. In an effort to help future noob programmers like myself to understand Activities, here's a great simple tutorial that I found online that mapped it out for us beginners!

http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/

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

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

发布评论

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

评论(4

野侃 2024-11-13 09:19:15

您的“关于”页面有单独的活动吗?通常,您会为每个屏幕创建一个新活动。当您转到新屏幕时,新活动将堆叠在第一个屏幕活动之上。当您在 Android 设备上单击“返回”时,将弹出上一个 Activity。

Do you have a separate activity for your about page? Generally for each screen you create a new activity. When you go to new screen , the new activity will be stacked over the first screen activity. When you click Back on android device, the previous activity will be popped up.

单调的奢华 2024-11-13 09:19:15

您想要做的是为每个不同的屏幕创建一个新的 Activity 。您将每个新 Activity 创建为新类,并使用 Intents 在之间移动他们。这样,您将为每个 Activity 仅使用一次 setContentView();Android 网站 充满了丰富的资源。

What you want to do is create a new Activity for every different screen. You create each new Activity as a new class and use Intents to move between them. This way you will use setContentView(); only once for each Activity. The Android website is full of great resources.

念﹏祤嫣 2024-11-13 09:19:15

您的主要活动将 contentview 设置为 main.xml,并且您还有另一个 about.xml,应将其设置为另一个活动,以便您可以使用以下方式从一个活动移动到另一个活动:意图。我建议您浏览开发者的网站,在那里您可以找到 Activity 和 Intent 的用法。

Your main activity has the contentview set to main.xml and you have another about.xmlwhich should be set to another activity so that you can move from one activity to another using Intents. I suggest you to kindly go through the developer's website where you can find the usage of activity and intents.

|煩躁 2024-11-13 09:19:15

当在这样的布局之间切换时,您需要管理后退按钮行为,

这里是一个在活动主类中使用的伪代码示例

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if((keyCode == KeyEvent.KEYCODE_BACK) && (secondary_layout_is_displayed))
    {
        displayPrimaryLayout();
        // return true to let the system know we consumed the back button press
        return true;
    }

    // return the default value
    return super.onKeyDown(keyCode, event);
}

When switching between layout like this you need to manage the back buttons behavior

here is a pseudocode example to use in your activities main class

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if((keyCode == KeyEvent.KEYCODE_BACK) && (secondary_layout_is_displayed))
    {
        displayPrimaryLayout();
        // return true to let the system know we consumed the back button press
        return true;
    }

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