就如上次所說的,如果改變了系統螢幕的設定方向,我們不妨可以這麼認為,它算是一個觸發事件的開始吧,那麼假使有人觸發了這個事件,我們是否能夠直接進行 某些操作呢,如改變介面的UI等?當然不行,因為上面的範例,我們直接是藉助setRequestedOrientation這個方法設定的,而在這個方 法中無法實現其他的操作。
android中,我們藉助的是另外一個事件——onConfigerationChanged,這個方法也是能夠重寫的。
示範代碼功能很簡單,就是在改變螢幕的方向的同時,也改變了點擊按鈕的text:
package com.mobile.allove.wfp;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class onConfigurationChangedTest extends Activity implements OnClickListener{
private Button mButton;
int intCurrentOrientation;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.Init();
mButton.setOnClickListener(this);
}
public void Init()
{
mButton=(Button) this.findViewById(R.id.Button01);
intCurrentOrientation=this.getRequestedOrientation();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.Button01:
if(this.intCurrentOrientation==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
{
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if(this.intCurrentOrientation==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
{
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
break;
}
}
@Override
public void setRequestedOrientation(int requestedOrientation) {
// TODO Auto-generated method stub
super.setRequestedOrientation(requestedOrientation);
}
@Override
public int getRequestedOrientation() {
// TODO Auto-generated method stub
return super.getRequestedOrientation();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT)
{
mButton.setText("現在是豎屏");
System.out.println("435435435456454");
}
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
{
mButton.setText("現在是橫屏");
System.out.println("kjkjhugggtvg");
}
super.onConfigurationChanged(newConfig);
}
}
這裡需要注意幾點,就是首先要設定初始的Orientation,而且還要設定捕捉更改的許可權—— Android.permission.CHANGE_CONFIGURATION,還有一點,就是必須在Activity裡設定 configChanges屬性。這裡我們不妨理解為聲明!!為了好理解,我也把AndroidManifest.xml檔案貼在這裡了
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobile.allove.wfp" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".onConfigurationChangedTest"
android:label="@string/app_name" android:screenOrientation="portrait"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
</manifest>