当前位置: 代码迷 >> Android >> Android按返回键(后退键)Back键事件捕获的两种步骤
  详细解决方案

Android按返回键(后退键)Back键事件捕获的两种步骤

热度:12   发布时间:2016-04-28 01:00:40.0
Android按返回键(后退键)Back键事件捕获的两种方法
package zhangphil.back;import android.support.v7.app.ActionBarActivity;import android.util.Log;import android.view.KeyEvent;import android.os.Bundle;public class MainActivity extends ActionBarActivity {	// 打印事件的标签	private final String TAG = "Phil";	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		Log.d(TAG, "onCreate()");	}	@Override	protected void onStart() {		super.onStart();		Log.d(TAG, "onStart()");	}	@Override	protected void onRestart() {		super.onRestart();		Log.d(TAG, "onRestart()");	}	@Override	protected void onResume() {		super.onResume();		Log.d(TAG, "onResume()");	}	@Override	protected void onPause() {		super.onPause();		Log.d(TAG, "onPause()");	}	@Override	protected void onStop() {		super.onStop();		Log.d(TAG, "onStop()");	}	@Override	protected void onDestroy() {		super.onDestroy();		Log.d(TAG, "onDestroy()");	}	// 捕获返回键的方法1	@Override	public boolean onKeyDown(int keyCode, KeyEvent event) {		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {			// 按下BACK,同时没有重复			Log.d(TAG, "onKeyDown()");		}		return super.onKeyDown(keyCode, event);	}	// 捕获返回键的方法2	@Override	public void onBackPressed() {		Log.d(TAG, "onBackPressed()");		super.onBackPressed();	}}

  相关解决方案