onConfigurationChanged資訊處理

來源:互聯網
上載者:User

     就如上次所說的,如果改變了系統螢幕的設定方向,我們不妨可以這麼認為,它算是一個觸發事件的開始吧,那麼假使有人觸發了這個事件,我們是否能夠直接進行 某些操作呢,如改變介面的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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.