在前面的范例中,我们看到了屏幕方向的更改,事实上,当屏幕方向改变时,就会发生onConfigurationChanged事件。虽然可以在更改方向时,显示要更改的方向,但是并无法取得更改后的宽高或更改后的结果,此时,就必须通过onConfigurationChanged的信息事件进行处理。 以下的范例为重写Activity里的onConfigurationChanged信息事件,并捕捉屏幕画面方向改变后的事件,在当下更改按钮上的文字,与将屏幕的分辨率显示在TextView上,如此一来,便能完全掌握更改屏幕之后的按钮文字,以及正确捕捉切换画面后的屏幕分辨率,这样即可适用于Layout的重新部署,或者搭配手机旋转事件应用。 onConfigurationChanged()方法,是当系统发生系统设置改变之后所触发的事件,其中唯一的传入参数为Configuration对象,除了可捕捉屏幕设置更改事件之外,也可捕捉其他系统设置更改的事件,如隐藏键盘或打开键盘等。
按钮事件毕竟是调用更改屏幕的起点,假如将访问屏幕分辨率的语句编写在按钮事件当中,就无法确认正确的屏幕方向,据此更改按钮文字。以下的方法是通过重写onConfigurationChanged()事件,在系统设置改变的当下,在onConfigurationChanged()里,依据更改结果显示按钮文字。如当画面变成了Configuration.ORIENTATION_LANDSCAPE,则将按钮显示文字改为"按我旋转为PORTRAIT";如果发生了Configuration.ORIENTATION_PORTRAIT,则将按钮显示文字改为"按我旋转为LANDSCAPE"。
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;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(); }}
}
AndroidManifest.xml 必须要在Activity里设置configChanges属性,以作为系统设置更改时要捕捉的事件;除此之外,还需要取得系统设置更改的权限(android.permission.CHANGE_CONFIGURATION)。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android package="irdc.ex05_23" android:versionCode="1" android:versionName="1.0.0"> <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> <!-- 必须设置CHANGE CONFIGURATION权限 --> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/> <uses-sdk android:minSdkVersion="7" /> </manifest>
onConfigurationChanged的参数如下:
public void onConfigurationChanged(Configuration newConfig) 其中唯一的参数newConfig,是手机更改的新设置,类型为Configuration效果图