当前位置:操作系统 > 安卓/Android >>

Android[中级教程]第一章 数据存储之Shared Preferences

 

看完了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; 

    @Override 

    protected void onCreate(Bundle savedInstanceState) 

    { 

        // TODO Auto-generated method stub 

        super.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

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,