[轉載]Activity中ConfigChanges屬性的用法

來源:互聯網
上載者:User
 通過設定這個屬性可以使Activity捕捉裝置狀態變化,以下是可以被識別的內容:  
CONFIG_FONT_SCALE
CONFIG_MCC
CONFIG_MNC
CONFIG_LOCALE
CONFIG_TOUCHSCREEN
CONFIG_KEYBOARD
CONFIG_NAVIGATION
CONFIG_ORIENTATION

設定方法:將下欄欄位用“|”符號分隔開,例如:“locale|navigation|orientation

Value Description
“mcc“ The IMSI mobile country code (MCC) has changed — that is, a SIM hasbeen detected and updated the MCC.移動國家號碼,由三位元字組成,每個國家都有自己獨立的MCC,可以識別手機使用者所屬國家。
“mnc“ The IMSI mobile network code (MNC) has changed — that is, a SIM hasbeen detected and updated the MNC.移動網號,在一個國家或者地區中,用於區分手機使用者的服務商。
“locale“ The locale has changed — for example, the user has selected a new language that text should be displayed in.使用者所在地區發生變化。
“touchscreen“ The touchscreen has changed. (This should never normally happen.)
“keyboard“ The keyboard type has changed — for example, the user has plugged in an external keyboard.鍵盤模式發生變化,例如:使用者接入外部鍵盤輸入。
“keyboardHidden“ The keyboard accessibility has changed — for example, the user has slid the keyboard out to expose it.使用者開啟手機硬體鍵盤
“navigation“ The navigation type has changed. (This should never normally happen.)
“orientation“ The screen orientation has changed — that is, the user has rotated the device.裝置旋轉,橫向顯示和豎向顯示模式切換。
“fontScale“ The font scaling factor has changed — that is, the user has selected a new global font size.全域字型大小縮放發生改變

通過一個例子介紹這個屬性的用法: 首先需要修改項目的manifest:
?View Code XML

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidres.ConfigChangedTesting"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ConfigChangedTesting"
                  android:label="@string/app_name"
                  android:configChanges="keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

在Activity中添加了 android:configChanges屬性,目的是當所指定屬性(Configuration Changes)發生改變時,通知程式調用 onConfigurationChanged()函數。 建立一個Layout UI:
?View Code XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
        android:id="@+id/pick"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Pick"
    />
<Button
        android:id="@+id/view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="View"
    />
</LinearLayout>

這個簡單的UI包含兩個按鈕,其中一個是通過Contact列表選擇一個連絡人,另外一個是查看當前選擇連絡人的詳細內容。

項目的Java原始碼:
01.import android.app.Activity;
02.import android.content.Intent;
03.import android.content.res.Configuration;
04.import android.net.Uri;
05.import android.os.Bundle;
06.import android.provider.Contacts.People;
07.import android.view.View;
08.import android.widget.Button;
09.

10.public class ConfigChangedTesting extends Activity {
11.    /** Called when the activity is first created. */
12.    static final int PICK_REQUEST = 1337;
13.    Button viewButton=null;
14.    Uri contact = null;
15.    @Override
16.    public void onCreate(Bundle savedInstanceState) {
17.        super.onCreate(savedInstanceState);
18.        //setContentView(R.layout.main);
19.

20.        setupViews();
21.    }
22.

23.    public void onConfigurationChanged(Configuration newConfig) {
24.                 super.onConfigurationChanged(newConfig);  
25.

26.                 setupViews();
27.    }  
28.

29.    /* (non-Javadoc)
30.     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
31.     */
32.    @Override
33.    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
34.        // TODO Auto-generated method stub
35.        //super.onActivityResult(requestCode, resultCode, data);
36.

37.        if(requestCode == PICK_REQUEST){
38.

39.            if(resultCode==RESULT_OK){
40.

41.                contact = data.getData();
42.                viewButton.setEnabled(true);
43.            }
44.

45.        }
46.

47.    }
48.

49.    private void setupViews(){
50.

51.        setContentView(R.layout.main);
52.

53.        Button pickBtn = (Button)findViewById(R.id.pick);
54.

55.        pickBtn.setOnClickListener(new View.OnClickListener(){
56.

57.            public void onClick(View v) {
58.                // TODO Auto-generated method stub
59.

60.                Intent i=new Intent(Intent.ACTION_PICK,People.CONTENT_URI);
61.                startActivityForResult(i,PICK_REQUEST);
62.            }
63.        });
64.

65.        viewButton =(Button)findViewById(R.id.view);  
66.

67.        viewButton.setOnClickListener(new View.OnClickListener() {
68.                    public void onClick(View view) {
69.                        startActivity(new Intent(Intent.ACTION_VIEW, contact));
70.                    }
71.        });  
72.

73.        viewButton.setEnabled(contact!=null);
74.    }
75.}

文章來源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.