博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
onConfigurationChanged信息处理
阅读量:6910 次
发布时间:2019-06-27

本文共 4374 字,大约阅读时间需要 14 分钟。

hot3.png

 在前面的范例中,我们看到了屏幕方向的更改,事实上,当屏幕方向改变时,就会发生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
效果图

转载于:https://my.oschina.net/bintojojo/blog/284978

你可能感兴趣的文章
年月日下拉选择三级联动(闰年判断),时间获取方法总结,特殊:获取当前月天数...
查看>>
Tallest Cow(POJ3263)
查看>>
POJ—Building a Space Station
查看>>
杭电oj Problem-1013 Digital Roots
查看>>
CRM 2013 切换显示语言
查看>>
Codeforces Round #544 (Div. 3) C. Balanced Team
查看>>
UML-对象图
查看>>
【leetcode】1037. Valid Boomerang
查看>>
一起学Android之Layout
查看>>
PHP网页计时工具——SESSION问题
查看>>
PHP 真正多线程的使用
查看>>
ip黑白名单防火墙frdev的原理与实现
查看>>
ajax全接触
查看>>
ps查看内存占用排序
查看>>
【BZOJ】4873: [Shoi2017]寿司餐厅
查看>>
leetcode-852-山脉数组的峰顶索引
查看>>
你了解HTTPS,但你可能不了解X.509
查看>>
Bootstrap 类解析
查看>>
项目总结12:bootstrap-select下拉框模糊搜索
查看>>
SCRUM 是一个用于开发和维护复杂产品的框架
查看>>