在前一個例子中我們看到了螢幕方向的更改,事實上,當螢幕方向改變是,就會發生onConfigurationChanged()事件;雖然可以在更改方向是顯示要更改的方向,但是並無法取得更改後的寬高或更改後的結果,此時,就必須通過onConfigurationChanged()的心事事件進行處理。
onConfigurationChanged()方法是當系統發生系統設定改變之後所觸發的事件,其中唯一的傳入參數為Configuration對象,出來可以捕捉螢幕設定變更事件之外,也可撲捉其他系統設定變更事件,如隱藏鍵盤或開啟鍵盤等。
Java代碼
public class EX05_23 extends Activity
{
private TextView mTextView01;
private Button mButton01;
/* 螢幕寬高*/
private int intScreenH,intScreenW;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 聲明Display對象,以取得螢幕寬高*/
final Display defaultDisplay = getWindow().getWindowManager().getDefaultDisplay();
intScreenH= defaultDisplay.getHeight();
intScreenW = defaultDisplay.getWidth();
mButton01 = (Button)findViewById(R.id.myButton1);
mTextView01 = (TextView)findViewById(R.id.myTextView1);
mTextView01.setText(Integer.toString(intScreenW)+"x"+Integer.toString(intScreenH));
/* 當寬>高,表示為橫式顯示*/
if(intScreenW > intScreenH)
{
/* Landscape */
mButton01.setText(R.string.str_button2);
}
else
{
/* Portrait */
mButton01.setText(R.string.str_button1);
}
/* 按鈕事件處理切換寬高*/
mButton01.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
intScreenH= defaultDisplay.getHeight();
intScreenW = defaultDisplay.getWidth();
/* 如果為Landscape */
if(intScreenW > intScreenH)
{
/* Landscape => Portrait */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else
{
/* Portrait => Landscape */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
/* 再一次取得螢幕寬高*/
intScreenH= defaultDisplay.getHeight();
intScreenW = defaultDisplay.getWidth();
mTextView01.setText(Integer.toString(intScreenW)+"x"+Integer.toString(intScreenH));
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
// TODO Auto-generated method stub
/* 覆寫onConfigurationChanged事件,捕捉當設定之後的值*/
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
/* 趁著事件發生之後,變更按鈕上的文字*/
mButton01.setText(R.string.str_button2);
mMakeTextToast
(
getResources().getText(R.string.str_onConf_LANDSCAPE).toString(),
false
);
}
/* 須設定configChanges屬性才能捕捉onConfigurationChanged */
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
mButton01.setText(R.string.str_button1);
mMakeTextToast
(
getResources().getText(R.string.str_onConf_PORTRAIT).toString(),
false
);
}
if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO)
{
}
super.onConfigurationChanged(newConfig);
}
public void mMakeTextToast(String str, boolean isLong)
{
if(isLong==true)
{
Toast.makeText(EX05_23.this, str, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(EX05_23.this, str, Toast.LENGTH_SHORT).show();
}
}
}
必須要在Activity裡設定configChanges屬性,以作為系統設定更改時要撲捉的事件,除此之外,還需要獲得系統設定更改的許可權(<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>)
Xml代碼
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<!-- 必須設定activity的configChanges屬性-->
<activity
android:name=".EX05_23"
android:label="@string/app_name"
android:configChanges="orientation|keyboard">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<!-- 必須設定CHANGE CONFIGURATION權限-->
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>