2012-02-04 62 views
1

我的Android應用程序出現問題。我有三個活動,全部通過圖像按鈕相互連接。無法返回按鈕

我的問題是我不能回去的任何頁面,只是轉發。我一直在閱讀關於後退按鈕以及如何保持其狀態並且不殺死(結束)它,所以用戶可以在需要時點擊回來。

我是Android新手,我知道有很多東西需要學習。所以謝謝你的幫助。針對第一個活動

Java代碼:

package my.hope; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageView; 
import android.content.Intent; 
public class NewhopeActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ImageView myImage = (ImageView) findViewById(R.id.imagebutton1); 
    myImage.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub   
       Intent intent = new Intent(NewhopeActivity.this, Act2.class); 
       startActivity(intent); 
      } 

}); 
} 

}

XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" android:background="@drawable/mint"> 


    <ImageButton 
    android:id="@+id/imagebutton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="250dp" 
    android:background="@drawable/ic_launcher" 
    android:onClick="Act2" 
    android:src="@drawable/ic_launcher" /> 

    </LinearLayout> 
+0

告訴我們您的Manifest.xml – 2012-02-04 15:49:15

+0

你可以上傳你的清單文件。我可以」沒有看到你的代碼有任何問題。後退按鈕應該起作用,因爲您只是從兒童活動回到父活動。 – 2012-02-04 15:50:35

+0

android:onClick \t \t當視圖被點擊時,在這個視圖的上下文中調用方法的名稱。 – L7ColWinters 2012-02-04 15:50:36

回答

2

當你點擊你的設備上的後退按鈕會發生什麼?除非指定,否則它會殺死當前活動並將您帶回上一個活動。 但是,如果你想指定正是將單擊後退按鈕做的,你可以覆蓋onBackPressed()方法:

@Override 
public void onBackPressed() { 
    Intent intent = new Intent(CurrentActivity.this, NextActivity.class); 
    startActivity(intent); 

    finish(); 
} 
+1

它不會立即殺死它,我認爲它只是把它從堆棧? – L7ColWinters 2012-02-04 15:53:52

+0

我沒有在我的任何xml佈局中的按鈕只是圖像按鈕(每個頁面上的1)如果我輸入一個按鈕和按鈕的意圖和上面的代碼將工作......謝謝 – ravon30 2012-02-04 18:39:00

+0

很高興我可以幫助,儘管我提到了所有Android設備附帶的物理後退按鈕:) – shaylh 2012-02-05 09:17:59