[Android Samples視頻系列之ApiDemos] App-Activity-PersistentState

來源:互聯網
上載者:User
App-Activity-PersistentState1.Demo說明與示範

    該Demo使用SharedPreference來儲存UI狀態,主要是為了示範SharedPreference的簡單使用,而且UI狀態我們一般在onSaveInstanceState中儲存。

    通過該Demo我們能學習到:

 

  •  SharedPreference的簡單使用
  • PreferenceManage的介紹
  • ScrollView的使用
  • HorizontalScrollView的介紹

 

    如下:

2.視頻講解與高清版視頻下載

http://www.eyeandroid.com/forum.php?mod=viewthread&tid=10285&extra=page%3D1

3.Demo分析

    Android 提供了多種儲存資料的方法,其中最簡單的是使用Shared Preferences. SharedPreferences 可以儲存 Key/value 對,SharedPreferences 支援存取 boolean, float ,long ,integer, string ,最常用的使用Shared Preferences是用來儲存一些應用偏好。此外的一個方法是使用onSaveInstanceState(),這是特別用來儲存UI 狀態的。

    App->Activity->Persistent State使用了Shared Preferences來保持部分UI狀態(TextView的值)。

建立或是修改SharedPreferences,使用getSharedPreferences(String name, intmode)方法。Shared Preferences 用於單個Application不同Activity之間共用一些資料,單不能用於不同Application之間共用資料。

    SharedPreferences.Editor 用來給Shared Preferences添加資料: editor.putXXX(key,value)

    /**

     * Any time we are paused we need to save away the current state, so    it will be restored correctly when we are resumed.

     */

    @Override

    protectedvoid onPause() {

        super.onPause();

       

        SharedPreferences.Editor editor = getPreferences(0).edit();

        editor.putString("text", mSaved.getText().toString());

        editor.putInt("selection-start", mSaved.getSelectionStart());

        editor.putInt("selection-end", mSaved.getSelectionEnd());

        editor.commit();

    }

    讀取Shared Preference: pref.getXXX(key)

   

/**

     * Upon being resumed we can retrieve the current state.  This allows us to update the state if it was changed at any time while paused.

     */

    @Override

    protectedvoid onResume() {

        super.onResume();

        SharedPreferences prefs = getPreferences(0);

        String restoredText = prefs.getString("text", null);

        if (restoredText != null) {

            mSaved.setText(restoredText, TextView.BufferType.EDITABLE);

 

            int selectionStart = prefs.getInt("selection-start", -1);

            int selectionEnd = prefs.getInt("selection-end", -1);

            if (selectionStart != -1 && selectionEnd != -1) {

                mSaved.setSelection(selectionStart, selectionEnd);

            }

        }

    }

 4.這個Demo我們學會了

1. Android資料存放區之SharedPreferences

http://www.eyeandroid.com/forum.php?mod=viewthread&tid=9608

2. Android SharedPreference模式的使用

http://www.eyeandroid.com/forum.php?mod=viewthread&tid=7533

3. Android控制項之ScrollView

http://www.eyeandroid.com/forum.php?mod=viewthread&tid=7615

4. android.view.View

http://www.eyeandroid.com/forum.php?mod=viewthread&tid=10160

5. ScrollView Refrence

http://www.eyeandroid.com/forum.php?mod=viewthread&tid=10161

 

5.官方答疑網站

Android開發論壇:http://www.eyeandroid.com

Android您問我講:http://www.eyeandroid.com/forum.php?mod=forumdisplay&fid=195

相關文章

聯繫我們

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