Android開發--SharedPreferences初步介紹

來源:互聯網
上載者:User

    儲存資料在Android開發中是一項非常重要的功能,下面介紹的這種技術是利用XML檔案儲存體索引值對。      SharedPreferences的使用主要有四步:            1)獲得SharedPreferences對象            2)獲得SharedPrefercences.Editor對象            3)使用putXXX方法儲存資料            4)將資料儲存在檔案中 下面的這個例子利用SharedPreferences實現儲存簡單的資料,儲存的內容為test.xml,檔案中的內容如下  [html]  www.2cto.com<?xml version='1.0' encoding='utf-8' standalone='yes' ?>  <map>  <string name="name">zhanghu</string>  <string name="habit">android,surfing,playing basketball</string>  </map>   下面是實現的: 具體的實現代碼如下:  [java] public class SharedPreferences_Activity extends Activity {      private EditText editText1,editText2,editText3,editText4;      private Button button1,button2;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_shared_preferences_);          editText1=(EditText)findViewById(R.id.editname);          editText2=(EditText)findViewById(R.id.edithabbit);          editText3=(EditText)findViewById(R.id.editname2);          editText4=(EditText)findViewById(R.id.edithabbit2);          button1=(Button)findViewById(R.id.commit);          button2=(Button)findViewById(R.id.display);          button1.setOnClickListener(new OnClickListener() {              @Override              public void onClick(View v) {                  // TODO Auto-generated method stub                  //獲得SharedPreferences對象(第一步)                  SharedPreferences mySharedPreferences=getSharedPreferences("test", Activity.MODE_PRIVATE);                  //獲得SharedPrefercences.Editor對象(第二步)                  SharedPreferences.Editor editor=mySharedPreferences.edit();                  //使用putXXX方法儲存資料(第三步)                  editor.putString("name", editText1.getText().toString());                  editor.putString("habit", editText2.getText().toString());                  //將資料儲存在檔案中(第四步)                  editor.commit();                  editText1.setText("");                  editText2.setText("");              }          });          button2.setOnClickListener(new OnClickListener() {                            @Override              public void onClick(View v) {                  // TODO Auto-generated method stub              SharedPreferences sharedPreferences=getSharedPreferences("test", Activity.MODE_PRIVATE);              String nameString=sharedPreferences.getString("name", "");              String habitString=sharedPreferences.getString("habit", "");              editText3.setText(nameString);              editText4.setText(habitString);              }          });      }        @Override      public boolean onCreateOptionsMenu(Menu menu) {          // Inflate the menu; this adds items to the action bar if it is present.          getMenuInflater().inflate(R.menu.activity_shared_preferences_, menu);          return true;      }    }   

聯繫我們

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