频道栏目
首页 > 资讯 > Android > 正文

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

11-10-04        来源:[db:作者]  
收藏   我要投稿

 

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

         

    } 

     

    //按钮事件监听 

    @Override 

    public void onClick(View v) 

    { 

        switch(v.getId()){ 

        //悟空又打死了一个妖怪按钮事件 

        case R.id.addOne: 

             

            //妖怪数量加1 

            count++; 

            //刷新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/你自己创建的包

摘自:kangkangz4的专栏

相关TAG标签
上一篇:Android[中级教程]第二章 数据存储之File
下一篇:获取手机通讯录信息方法总结
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站