android SharedPreferences簡單應用 插入資料 讀取資料

來源:互聯網
上載者:User

標籤:android   style   blog   color   java   os   io   檔案   

package com.sharedpreference;    import java.text.SimpleDateFormat;  import java.util.Date;  import android.os.Bundle;  import android.app.Activity;  import android.content.SharedPreferences;  import android.view.Menu;  import android.view.View;  import android.view.View.OnClickListener;  import android.widget.Button;  import android.widget.Toast;    public class SharedPreferenceActivity extends Activity {        SharedPreferences preferences;            //Editor放入值      SharedPreferences.Editor editor;      private Button btnWrite;      private Button btnRead;            @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          btnRead = (Button)findViewById(R.id.read);          btnWrite = (Button)findViewById(R.id.write);          //context提供的getSharedPreferences獲得sharedPreferences執行個體          preferences = getSharedPreferences("bang", MODE_PRIVATE);          editor = preferences.edit();          btnRead.setOnClickListener(new OnClickListener() {                            @Override              public void onClick(View arg0) {                  //讀取字串資料型別                  String time = preferences.getString("time",null);                                    //讀取int資料類型                  int randNum = preferences.getInt("randrom", 0);                                    String result = time == null ? "您暫時還沒寫入資料" : "寫入時間為:"+time+"\n上次產生的隨機數為:"+randNum;                                    Toast.makeText(SharedPreferenceActivity.this, result,5000).show();              }          });                    btnWrite.setOnClickListener(new OnClickListener() {                            @Override              public void onClick(View arg0) {                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"+"hh:mm:ss");                                    editor.putString("time", sdf.format(new Date()));                                    editor.putInt("randrom", (int)(Math.random()*100));                  //提交儲存的資料                  editor.commit();              }          });                }          @Override      public boolean onCreateOptionsMenu(Menu menu) {          // Inflate the menu; this adds items to the action bar if it is present.          getMenuInflater().inflate(R.menu.main, menu);          return true;      }        }  

產生的shared_prefs下的xml檔案為

<?xml version=‘1.0‘ encoding=‘utf-8‘ standalone=‘yes‘ ?>  <map>      <string name="time">2014年04月14日01:10:21</string>      <int name="randrom" value="20" />  </map>  

SharedPreferences 儲存簡單資料 儲存索引值對類型資料

聯繫我們

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