Android 如何按“后退”按钮返回上一个菜单?-安卓

Android 如何按“后退”按钮返回上一个菜单?-安卓,android,android-activity,scrollview,swipe,Android,Android Activity,Scrollview,Swipe,我正在开发一个应用程序,这是代码。我有两个问题 mainactivity.java package com.shashank.sharjahinternationalairport; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import a

我正在开发一个应用程序,这是代码。我有两个问题

mainactivity.java

package com.shashank.sharjahinternationalairport;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

    public class MainActivity extends Activity {
ImageButton flightInfoButton;
ImageButton airportGuideButton;
ImageButton visitorInfoButton;
ImageButton saaDcaButton;
ImageButton cargoButton;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    flightInfoButton = (ImageButton) findViewById(R.id.flightInfo);
    airportGuideButton = (ImageButton) findViewById(R.id.airportGuide);
    visitorInfoButton = (ImageButton) findViewById(R.id.visitorInfo);
    saaDcaButton = (ImageButton) findViewById(R.id.saaDca);        
    cargoButton = (ImageButton) findViewById(R.id.cargo);

    airportGuideButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View V){

        setContentView(R.layout.airport_guide);

    }
  });
    visitorInfoButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View V){

            setContentView(R.layout.visitor_info);

        }
    });
    saaDcaButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View V){

            setContentView(R.layout.saa_dca);

        }
    });
  }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
这就是java代码。在进入机场指南、saa\U dca或访客信息后,如果我按下后退按钮,应用程序将关闭,而不是返回主菜单。这是否与活动生命周期有关?我是否应该在其中包含一些内容?如果是,请告诉我怎么做。我是android开发新手

这是saa_dca.xml代码

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <ImageButton
    android:id="@+id/corporateProfile"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
 <ImageButton
    android:id="@+id/infoForAirline"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
 <ImageButton
    android:id="@+id/businessPolicy"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
 <ImageButton
    android:id="@+id/saftyAndSecurity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
     <ImageButton
    android:id="@+id/trafficRights"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
 <ImageButton
    android:id="@+id/securityPasses"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
 <ImageButton
    android:id="@+id/photography"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
 <ImageButton
    android:id="@+id/mediaCentre"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
     <ImageButton
    android:id="@+id/customerRelations"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
 <ImageButton
    android:id="@+id/contactUs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
  </LinearLayout>

正如你所看到的,这里有十个图像按钮,但是当点击相应的按钮后打开时,所有的按钮都不适合屏幕,我认为android默认情况下会提供上下滚动,如果按钮超过屏幕,但显然不是这样。那么,我如何包括滑动功能,使其上下滚动?我应该把它包括在哪里?如果你想要其他的xml代码,请询问。我也会把它们放进去


提前谢谢。

不确定,但我的假设是,您可以覆盖onBackPressed()

这个


您可以跟踪当前按下的按钮。当你点击后退按钮时,你知道被跟踪的位置,比如说它的第五个位置,只需聚焦第四个位置的按钮

如果菜单在片段中,可以将其添加到后堆栈中

然后,当您按下“后退”按钮时,它将返回到上一个菜单,然后退出(在另一个后退按钮上按下)

 @Override
public void onBackPressed() {

}