通过单击按钮返回 Android 中的主菜单

发布于 2024-10-04 00:10:42 字数 298 浏览 0 评论 0原文

在android中,我的应用程序提供了一个按钮,用户可以单击该按钮将其返回到打开应用程序(onCreate)时出现的屏幕。

如何设置该按钮以使用户返回主菜单?

我在 switch 语句中有这样的内容(点击时):

         case R.id.return_main:
            onCreate();
            return;

其中 return_main 是按钮的 id....我知道这是不对的,但我想不出任何其他方法。

谢谢!

In android, my app provides a button that the user can click to return them back to the screen that appears when the app is opened (onCreate).

How can I set that button to return the user to the main menu?

I have something like this in a switch statement (on click):

         case R.id.return_main:
            onCreate();
            return;

Where return_main is the id of the button....I know that isn't right but I couldn't think of any other way.

Thanks!

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

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

发布评论

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

评论(4

疯到世界奔溃 2024-10-11 00:10:42

使用意图重新启动您的主要活动:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

Use an intent to re-launch your main activity:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
毁虫ゝ 2024-10-11 00:10:42

您应该能够调用 finish()。如果您所在的活动是主活动的子活动,这将使您返回到该主活动屏幕。

You should be able to just call finish(). If you are in an Activity that is a child of your main Activity, this will return you to that main Activity screen.

拿命拼未来 2024-10-11 00:10:42

Intent意图 = new Intent(this, MainActivity.class);
启动活动(意图);

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

╭ゆ眷念 2024-10-11 00:10:42

您必须杀死该活动才能返回主菜单,例如我从菜单调用 Activity1 并杀死它:

ActMenu.java

startActivity(new Intent(ActMenu.this,Activity1.class));

Activity1.java

Button btnForm = (Button) this.findViewById(R.id.btnForm);      
        btnForm.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                finish();                           
            }
        });

You have to kill the activity to return to the Main Menu, for example I called Activity1 from Menu and kill it:

ActMenu.java

startActivity(new Intent(ActMenu.this,Activity1.class));

Activity1.java

Button btnForm = (Button) this.findViewById(R.id.btnForm);      
        btnForm.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                finish();                           
            }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文