如何在按下后退按钮时返回到上一个菜单?-安卓系统



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

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代码。在这里,当它进入airport_guide、saa_ca或visitor_info后,如果我按下后退按钮,应用程序就会关闭,而不是返回主菜单。这与活动生命周期有关吗?我应该在其中包含一些内容吗?如果是,请告诉我怎么做。我是android开发的新手。

这是saa_ca.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() {
}

这个

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

如果菜单位于片段中,则可以将其添加到后堆栈中。

BackStack

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

相关内容

最新更新