部分文章内容为公开资料查询整理,原文出处可能未标注,如有侵权,请联系我,谢谢。邮箱地址:gnivor@163.com ►►►需要气球么?请点击我吧!

Android学习笔记--存储方案(SharedPreference、文件IO)

1. SharedPreference

SharedPreference可以很容易的保存key-value对,通常用于保存配置信息。
保存的步骤

1. 获得SharedPreferences对象  (最后一个参数指定了文件的建立模式,设置文件属性)
  SharedPreferences mySharedPreference = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
2. 获得SharedPreferences.Editor对象
  SharedPreferences.Editor editor = mySharedPreference.edit();
3. 保存组件中的值
  editor.putString("name",edtname.getText().toString());
  editor.putInt("age",Integer.valueOf(edtage.getText().toString()));
4. 提交保存的结果
  editor.commit();             


demo界面如下,实现的功能:点击保存信息将姓名、年龄保存。重启demo后,点击显示信息,将之前保存的内容显示到界面。

MainActivity.java

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    //SharedPreference可以很容易的保存key-value对,因此通常用于保存配置信息    
    //SharedPreference会将key-value对保存在survey.xml文件中
    private final String PREFERENCE_NAME = "survey";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText edtname = (EditText)findViewById(R.id.edt1);
        final EditText edtage = (EditText)findViewById(R.id.edt2);
        Button btn1 = (Button)findViewById(R.id.btn1);
        Button btn2 = (Button)findViewById(R.id.btn2);
        btn1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                //1. 获得SharedPreferences对象  (最后一个参数指定了文件的建立模式,设置文件属性)
                SharedPreferences mySharedPreference = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
                //2. 获得SharedPreferences.Editor对象
                SharedPreferences.Editor editor = mySharedPreference.edit();
                //3. 保存组件中的值
                editor.putString("name",edtname.getText().toString());
                editor.putInt("age",Integer.valueOf(edtage.getText().toString()));
                //4. 提交保存的结果
                editor.commit();                        
                
                Toast.makeText(MainActivity.this, "信息已保存", Toast.LENGTH_SHORT).show();
            }            
        });
        
        btn2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                //获得SharedPreferences对象
                SharedPreferences mySharedPreference = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
                //获得保存的值
                String name = mySharedPreference.getString("name", "");
                int age = mySharedPreference.getInt("age", 0);
                Toast.makeText(MainActivity.this, "姓名:"+name+" 年龄:"+age, Toast.LENGTH_SHORT).show();
                
                edtname.setText(name);
                edtage.setText(String.valueOf(age));
            }            
        });       
    }
    
    @Override
    protected void onStop(){
        super.onStop();        
    }
}
View Code

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.andtest008sharedpreferences.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名" />    
    <EditText
        android:id="@+id/edt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint = "请输入姓名"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="年龄" />
    <EditText
        android:id="@+id/edt2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint = "请输入年龄"
        />    
    <Button
        android:id = "@+id/btn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="保存信息"
        />    
    <Button
        android:id = "@+id/btn2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="显示信息"
        />  
</LinearLayout>
View Code

 

2.文件IO

使用的工具OutputStream/InputStream

向file.txt写入内容
OutputStream outer = openFileOutput("file.txt",Activity.MODE_PRIVATE);

读取file.txt的内容
InputStream inner = openFileInput("file.txt");

package com.evor.test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        try {
            //向文件写入内容
            OutputStream outer = openFileOutput("file.txt",Activity.MODE_PRIVATE);
            String str = "Test:这是一行测试文字!!";
            outer.write(str.getBytes("utf-8"));
            outer.close();
            
            //读取文件的内容,并显示在textView
            InputStream inner = openFileInput("file.txt");
            byte[] buffer = new byte[100];
            int bytecount = inner.read(buffer);
            String str2 = new String(buffer,0,bytecount,"utf-8");
            TextView view = (TextView)findViewById(R.id.showtxt);
            view.setText(str2);
            inner.close();
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
}
View Code

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.evor.test.MainActivity" >

    <TextView
        android:id="@+id/showtxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>
View Code

 

posted @ 2015-11-07 14:32  流了个火  阅读(312)  评论(0编辑  收藏  举报
►►►需要气球么?请点击我吧!►►►
View My Stats