2-20 Android SharePreferences簡單使用(四)

來源:互聯網
上載者:User

對於資料的儲存有資料庫儲存,有sharePreferences這些方法

資料庫使用的是sqlite,而後者是將儲存的資料存入xml檔案中,這個檔案在/data/data/包名/shared_prefs中

我現在有一個很小的例子

兩個EditText,在那裡面寫入資料,然後點擊返回鍵,然後在點擊啟動應用,將會看到,在兩個EditText中內容還依然存在

private EditText eText_num,eText_word;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        eText_num=(EditText)findViewById(R.id.editText1);
        eText_word=(EditText)findViewById(R.id.editText2);
    }
    
    @Override
    protected void onResume() {
    super.onResume();
    SharedPreferences sp=getSharedPreferences("test", MODE_WORLD_READABLE);
    String num=sp.getString("num","");
    String word=sp.getString("word", "");
    eText_num.setText(num);
    eText_word.setText(word);
//     sp.edit().clear();
//     sp.edit().commit();
    }
    @Override
    protected void onPause() {
    super.onPause();
    SharedPreferences sp=getSharedPreferences("test", MODE_WORLD_WRITEABLE);
    Editor editor=sp.edit();
    editor.putString("num", eText_num.getText().toString());
    editor.putString("word", eText_word.getText().toString());
    editor.commit();
    }

相關文章

聯繫我們

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