編輯:關於android開發
1.android:configChanges="orientation|keyboardHidden"的使用
當在activity加上android:configChanges="keyboardHidden|orientation"屬性,就不會重啟activity.而只是調用onConfigurationChanged(Configuration newConfig).這樣就可以在這個方法裡調整顯示方式.
在xml文件裡面可以進行配置configChanges也可以在代碼中動態配置
注意: 1、不設置Activity的android:configChanges時,切屏會重新調用各個生命周期,切橫屏時會執行一次,切豎屏時會執行兩次
2、設置Activity的android:configChanges="orientation"時,切屏還是會重新調用各個生命周期,切橫、豎屏時只會執行一次
3、設置Activity的android:configChanges="orientation|keyboardHidden"時,切屏不會重新調用各個生命周期,只會執行onConfigurationChanged方法
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="12" />
2)在版本13+以上的時候必須添加上"|screenSize",即 android:configChanges="orientation|keyboardHidden|screenSize" 才會觸發onConfigurationChanged(Configuration newConfig)此方法
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="13" />
2.下面是屏幕切換的代碼:
Configuration cfg=getResources().getConfiguration();
if(cfg.orientation==Configuration.ORIENTATION_LANDSCAPE){
GetSystemInfoActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
if(cfg.orientation==Configuration.ORIENTATION_PORTRAIT){
GetSystemInfoActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
重寫chonConfigurationChanged(Configuration newConfig)方法觸發調用
@Override
public void onConfigurationChanged(Configuration cfg) {
super.onConfigurationChanged(cfg);
Toast.makeText(GetSystemInfoActivity.this, "test:"+(cfg.orientation==Configuration.ORIENTATION_LANDSCAPE?"橫屏":"豎屏"),Toast.LENGTH_SHORT).show();
}
3.屏幕切換時關閉軟鍵盤輸入法彈出 <activity ... android:windowSoftInputMode="adjustUnspecified|stateHidden" /> 或者在代碼裡面調用關閉輸入法軟鍵盤的代碼:
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
InputMethodManager inputManager =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(p, 0);
onConfigurationChanged為何不被調用?
英文原文如下:
Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
(From http://developer.android.com/guide/topics/resources/runtime-changes.html)
TL;DR: add "|screenSize" to configChanges when targeting API level 13+
參考:
1.http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1106/516.html
2.http://www.cnblogs.com/xiaokang088/p/3540189.html
3.http://blog.csdn.net/songshimvp1/article/details/50109879
4.http://www.cnblogs.com/androidez/archive/2013/04/09/3011399.html(關閉輸入法參考地址)
Android 塗鴉最佳實踐 Android中實現手勢畫圖一般都兩種方式,一是直接在View上繪制,而是使用SurfaceView。 兩者還是有一些差別的。簡介下。
Android SharedPreference的使用,sharedpreference在《Android 在內部存儲讀寫文件》一文中,談到了登錄用戶名和密碼的方法,通過
Android 5.0最應該實現的8個期望,android5.0期望毫無疑問,Android 5 將是令人興奮的操作系統,因為 Android4.0 至 4.4 版本之間
Android中Canvas繪圖之Shader使用圖文詳解 概述 我們在用Android中的Canvas繪制各種圖形時,可以通過Paint.setShader(shad