2014-02-10 72 views
0

我正在开发一个应用程序,这是代码。我有两个问题。如何按下后退按钮返回上一级菜单? - Android

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_dca或visitor_info之后,如果按下后退按钮,应用程序将关闭而不是返回主菜单。这是与活动生命周期有关的事情,我应该在那里包括什么?如果是这样,请告诉如何。我是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代码,请问。我也会把它们放在一起。

预先感谢您。

回答

1

不知道,但这里是我的假设,可以覆盖onBackPressed()

@Override 
public void onBackPressed() { 

} 

这个

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

1

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

BackStack

然后,当你按下它后面会回到上级菜单退出之前(在另一个后退按钮按下)