2011-01-27 100 views

回答

8

使用onKeyDown;

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 

      // Your Code Here 

     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
4

你可以捕捉的关键事件,并检查是否有返回键。在您的活动上:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if(keyCode == KeyEvent.KEYCODE_BACK){ 
     goBack(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

并写入goBack方法去你需要的地方。

更多在:Android - onBackPressed() not working

1

答案---> http://apachejava.blogspot.com/2011/01/backward-compatibility-using.html

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR 
      && keyCode == KeyEvent.KEYCODE_BACK 
      && event.getRepeatCount() == 0) { 
     // Take care of calling this method on earlier versions of 
     // the platform where it doesn't exist. 
     onBackPressed(); 
    } 

    return super.onKeyDown(keyCode, event); 
} 

@Override 
public void onBackPressed() { 
    // This will be called either automatically for you on 2.0 
    // or later, or by the code above on earlier versions of the 
    // platform. 
    return; 
} 
+0

该解决方案将无法工作。 2件事...一,android.os.Build.VERSION.SDK_INT在Android 1.5上不可用。二,你需要调用super.onBackPressed()否则后退按钮不起作用。调用super.onBackPressed()将不会在Android 1.5设备下找到并返回错误。 – Jona 2011-01-28 17:38:47