Android[中級教程]第一章 資料存放區之Shared Preferences

來源:互聯網
上載者:User

 看完了Android[初級教程],終於可以學習[中級教程]了,呵呵,這次我們就來學習Android開發中的資料存放區,首先我們來學習Shared Preferences,Shared Preferences只是簡單地儲存了資料的Key-Value值,相信學過java的人都知道其中有一種類型Map,也是以Key-Value的形式來儲存資料.但Shared Preferences跟Map有本質的區別,Map只存在於程式內部,而Shared Preferences是將資料存放區於硬體裝置上的(OK,這裡硬體裝置就是手機啦).好了,不多說了,我們用Shared
Preferences來實現儲存悟空打妖怪的數量,你不會想讓悟空每次一開程式就重新打妖怪吧?那估計悟空要找你了,呵呵

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:layout_width="wrap_content" android:id="@+id/textView1"android:textAppearance="?android:attr/textAppearanceLarge"android:layout_height="wrap_content" android:text="TextView"></TextView><EditText android:id="@+id/editText1" android:layout_width="match_parent"android:layout_height="wrap_content" android:inputType="number"><requestFocus></requestFocus></EditText><LinearLayout android:layout_width="match_parent"android:id="@+id/linearLayout2" android:layout_height="wrap_content"><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/addOne"android:text="悟空又打死了一個妖怪"></Button><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/read_edit"android:text="讀取Edit中的資料"></Button></LinearLayout><LinearLayout android:layout_width="match_parent"android:id="@+id/linearLayout1" android:layout_height="wrap_content"><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/save"android:text="儲存資料"></Button><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/read"android:text="讀取資料"></Button><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/clear"android:text="清除資料"></Button></LinearLayout></LinearLayout>

這裡面只是加了一個EditText和幾個按鈕,接下來看一下java源碼:

import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class Shared_PreferencesDemo extends Activity implements OnClickListener{private int count = 0;private String str;private TextView text;private EditText edit_text;private Button add;private Button save;private Button read;private View clear;private Button read_edit;private SharedPreferences preferences;private Editor edit;@Overrideprotected void onCreate(Bundle savedInstanceState){// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.shared_preferences);//擷取只能被本程式讀,寫的SharedPreferences對象preferences = getSharedPreferences("shared", 0);//擷取SharedPreferences的edit對象edit = preferences.edit();str = "悟空殺死了" + count + "只妖怪.";text = (TextView)findViewById(R.id.textView1);text.setText(str);edit_text = (EditText)findViewById(R.id.editText1);edit_text.setText(String.valueOf(count));add = (Button)findViewById(R.id.addOne);add.setOnClickListener(this);read_edit = (Button)findViewById(R.id.read_edit);read_edit.setOnClickListener(this);save = (Button)findViewById(R.id.save);save.setOnClickListener(this);read = (Button)findViewById(R.id.read);read.setOnClickListener(this);clear = (Button)findViewById(R.id.clear);clear.setOnClickListener(this);}//按鈕事件監聽@Overridepublic void onClick(View v){switch(v.getId()){//悟空又打死了一個妖怪按鈕事件case R.id.addOne://妖怪數量加1count++;//重新整理TextView和EditText控制項中的值refresh();break;//讀取Edit中的資料按鈕事件case R.id.read_edit://取出存在SharedPreferences中的值count =Integer.parseInt(edit_text.getText().toString());refresh();break;case R.id.save://將悟空打死妖怪的數量存入SharedPreferences中edit.putString("number", String.valueOf(count));edit.commit();break;case R.id.read://從SharedPreferences中取出悟空打死妖怪的數量count =Integer.valueOf(preferences.getString("number", "0"));refresh();break;case R.id.clear://清除SharedPreferences中所有的資料edit.clear();edit.commit();count = 0;refresh();break;}}//自訂重新整理private void refresh(){str = "悟空殺死了" + count + "只妖怪.";text.setText(str);edit_text.setText(String.valueOf(count));}}

好了,讓我們看一呢

資料存放區位置是data/data/你自己建立的包

OK,這一章又結束了,謝謝

聯繫我們

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