Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 按Android平板(或手機)的物理按鍵(如音量鍵)實現Activity界面的跳轉

按Android平板(或手機)的物理按鍵(如音量鍵)實現Activity界面的跳轉

編輯:關於Android編程

一.需求分析:

1.通過按Android平板(或手機)的物理按鍵(如音量鍵)實現Activity界面的跳轉;以前基本是自己寫控件進行跳轉啊!

2.按音量減鍵跳轉:界面1到界面2,2到3,3到4,4到5,5到1;按音量加鍵跳轉:1到5,5到4,4到3,3到2,2到1。

五個界面顏色1:白色,2:黑色,3:紅,4:綠,5:藍。 目的是用來進行屏的測試。

 

二、直接上代碼

1.五個類的代碼

Launcher1:

 

package com.zhc.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Launcher1 extends Activity implements KeyEvent.Callback {
	private static final String TAG = "Launcher";
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 1.隱藏標題欄
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 2.隱藏狀態欄
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		// 3.隱藏導航欄和狀態欄
		View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);

		setContentView(R.layout.activity_one);
		
	}

	// 4. 攔截系統熱鍵
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int key = event.getKeyCode();//獲取物理按鍵的key類型:比如音量鍵,power鍵等
		int key1 = event.getAction();//獲取某一物理按鍵的對應的事件類型;比如音量鍵的按下(down)事件,音量鍵的松開(up)事件
		if (key == KeyEvent.KEYCODE_DPAD_LEFT
				|| key == KeyEvent.KEYCODE_VOLUME_UP) {//按下的是安卓物理左鍵或者是音量鍵上鍵

			if (key1 == KeyEvent.ACTION_UP) {//音量鍵上鍵的up事件
				Log.i(TAG, "1");
				Intent intentright = new Intent(Launcher1.this, Launcher5.class);
				startActivity(intentright);

				return true;
			}
		} else if (key == KeyEvent.KEYCODE_DPAD_RIGHT
				|| key == KeyEvent.KEYCODE_VOLUME_DOWN) {//按下的是安卓物理右鍵或者是音量鍵下鍵

			if (key1 == KeyEvent.ACTION_UP) {//音量鍵下鍵的up事件
				Log.i(TAG, "2");
				Intent intentright = new Intent(Launcher1.this, Launcher2.class);
				startActivity(intentright);

				return true;
			}
		}

		return super.dispatchKeyEvent(event);
	}
}
Launcher2:
package com.zhc.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Launcher2 extends Activity {
	private static final String TAG = "Launcher2";

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 1.隱藏標題欄
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 2.隱藏狀態欄
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// 3.隱藏導航欄和狀態欄
		View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);

		setContentView(R.layout.activity_two);

	}

	// 攔截系統熱鍵
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int key = event.getKeyCode();// 獲取物理按鍵的key類型:比如音量鍵,power鍵等
		int key1 = event.getAction();// 獲取某一物理按鍵的對應的事件類型;比如音量鍵的按下(down)事件,音量鍵的松開(up)事件
		if (key == KeyEvent.KEYCODE_DPAD_LEFT
				|| key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左鍵或者是音量鍵上鍵

			if (key1 == KeyEvent.ACTION_UP) {// 音量鍵上鍵的up事件
				Log.i(TAG, "1");
				Intent intentright = new Intent(Launcher2.this, Launcher1.class);
				startActivity(intentright);

				return true;
			}
		} else if (key == KeyEvent.KEYCODE_DPAD_RIGHT
				|| key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右鍵或者是音量鍵下鍵

			if (key1 == KeyEvent.ACTION_UP) {// 音量鍵下鍵的up事件
				Log.i(TAG, "2");
				Intent intentright = new Intent(Launcher2.this, Launcher3.class);
				startActivity(intentright);

				return true;
			}
		}

		return super.dispatchKeyEvent(event);
	}

}
Launcher3:

 

 

package com.zhc.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Launcher3 extends Activity {
	private static final String TAG = "Launcher3";

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 1.隱藏標題欄
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 2.隱藏狀態欄
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// 3.隱藏導航欄和狀態欄
		View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);

		setContentView(R.layout.activity_three);

	}

	// 攔截系統熱鍵
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int key = event.getKeyCode();// 獲取物理按鍵的key類型:比如音量鍵,power鍵等
		int key1 = event.getAction();// 獲取某一物理按鍵的對應的事件類型;比如音量鍵的按下(down)事件,音量鍵的松開(up)事件
		if (key == KeyEvent.KEYCODE_DPAD_LEFT
				|| key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左鍵或者是音量鍵上鍵

			if (key1 == KeyEvent.ACTION_UP) {// 音量鍵上鍵的up事件
				Log.i(TAG, "1");
				Intent intentright = new Intent(Launcher3.this, Launcher2.class);
				startActivity(intentright);

				return true;
			}
		} else if (key == KeyEvent.KEYCODE_DPAD_RIGHT
				|| key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右鍵或者是音量鍵下鍵

			if (key1 == KeyEvent.ACTION_UP) {// 音量鍵下鍵的up事件
				Log.i(TAG, "2");
				Intent intentright = new Intent(Launcher3.this, Launcher4.class);
				startActivity(intentright);

				return true;
			}
		}

		return super.dispatchKeyEvent(event);
	}

}


 

Launcher4:

 

package com.zhc.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Launcher4 extends Activity {
	private static final String TAG = "Launcher4";

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 1.隱藏標題欄
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 2.隱藏狀態欄
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// 3.隱藏導航欄和狀態欄
		View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);

		setContentView(R.layout.activity_four);
	}

	// 攔截系統熱鍵
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int key = event.getKeyCode();// 獲取物理按鍵的key類型:比如音量鍵,power鍵等
		int key1 = event.getAction();// 獲取某一物理按鍵的對應的事件類型;比如音量鍵的按下(down)事件,音量鍵的松開(up)事件
		if (key == KeyEvent.KEYCODE_DPAD_LEFT
				|| key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左鍵或者是音量鍵上鍵

			if (key1 == KeyEvent.ACTION_UP) {// 音量鍵上鍵的up事件
				Log.i(TAG, "1");
				Intent intentright = new Intent(Launcher4.this, Launcher3.class);
				startActivity(intentright);

				return true;
			}
		} else if (key == KeyEvent.KEYCODE_DPAD_RIGHT
				|| key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右鍵或者是音量鍵下鍵

			if (key1 == KeyEvent.ACTION_UP) {// 音量鍵下鍵的up事件
				Log.i(TAG, "2");
				Intent intentright = new Intent(Launcher4.this, Launcher5.class);
				startActivity(intentright);

				return true;
			}
		}

		return super.dispatchKeyEvent(event);
	}

}


 

Launcher5:

 

package com.zhc.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Launcher5 extends Activity {
	private static final String TAG = "Launcher5";

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 1.隱藏標題欄
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 2.隱藏狀態欄
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// 3.隱藏導航欄和狀態欄
		View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);

		setContentView(R.layout.activity_five);
	}

	// 攔截系統熱鍵
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int key = event.getKeyCode();// 獲取物理按鍵的key類型:比如音量鍵,power鍵等
		int key1 = event.getAction();// 獲取某一物理按鍵的對應的事件類型;比如音量鍵的按下(down)事件,音量鍵的松開(up)事件
		if (key == KeyEvent.KEYCODE_DPAD_LEFT
				|| key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左鍵或者是音量鍵上鍵

			if (key1 == KeyEvent.ACTION_UP) {// 音量鍵上鍵的up事件
				Log.i(TAG, "1");
				Intent intentright = new Intent(Launcher5.this, Launcher4.class);
				startActivity(intentright);

				return true;
			}
		} else if (key == KeyEvent.KEYCODE_DPAD_RIGHT
				|| key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右鍵或者是音量鍵下鍵

			if (key1 == KeyEvent.ACTION_UP) {// 音量鍵下鍵的up事件
				Log.i(TAG, "2");
				Intent intentright = new Intent(Launcher5.this, Launcher1.class);
				startActivity(intentright);

				return true;
			}
		}

		return super.dispatchKeyEvent(event);
	}

}

2.對應的五個Xml:

 

 



    




 




    
 




     




    


 

3.清單文件

 

  
        
        
        

三、gif圖不知到弄,圖片不貼了,說的很清楚了。
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved