系统设置更改时间onConfigurationChanged
在前一个例子中我们看到了屏幕方向的更改,事实上,当屏幕方向改变是,就会发生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"
and
补充:移动开发 , Android ,