码迷,mamicode.com
首页 > 移动开发 > 详细

android小功能实现之简单数据持久化保存(SharedPreferences)

时间:2015-03-04 21:11:52      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:android开发



为了保存一些简单的配置,类似iOS的NSUserDefault和cocos2dx的CCUserDefault,Android提供了SharedPreferences。


一 布局
先看效果图:

技术分享


打开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="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/input_name" />

    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/input_name" />

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/input_age" />

    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numeric="integer"
        android:id="@+id/input_age" />

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_save"
        android:onClick="save"
        android:id="@+id/button_save"/>
</LinearLayout>  


二 定义字符串
打开strings.xml添加内容如下:
    <string name="input_name">姓名</string>
    <string name="input_age">年龄</string>
    <string name="button_save">保存参数</string>
    <string name="success">保存成功</string>
    <string name="fail">保存失败</string>


三 功能实现
修改MainActivity.java代码如下:
 
    private EditText nameText;
    private EditText ageText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nameText = (EditText)this.findViewById(R.id.input_name);
        ageText = (EditText)this.findViewById(R.id.input_age);
        // 读取保存的值
        //SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);// 默认使用类名作为文件名称
        //SharedPreferences preferences = this.getSharedPreferences("preference", Context.MODE_PRIVATE);
        SharedPreferences preferences = getApplicationContext().getSharedPreferences("preference", Context.MODE_PRIVATE);
        String name = preferences.getString("name", "空");
        String age = String.valueOf(preferences.getInt("age", 0));
        nameText.setText(name);
        ageText.setText(age);
    }

    public void save(View v){
        String name = nameText.getText().toString();
        Integer age = Integer.valueOf( ageText.getText().toString() );
        try{
            // 第一个参数为文件名称,不能指定后缀名,第二个参数为文件操作模式
            SharedPreferences preferences = getApplicationContext().getSharedPreferences("preference", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("name", name);
            editor.putInt("age", age);
            editor.commit();
            Toast.makeText(getApplicationContext(),R.string.success, Toast.LENGTH_LONG).show();
        }
        catch (Exception e){
            Toast.makeText(getApplicationContext(),R.string.fail, Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }


四 运行结果

第一次运行结果如图:

技术分享

输入内容,点击保存参数按钮,退出程序,再次打开,如图:

技术分享


android小功能实现之简单数据持久化保存(SharedPreferences)

标签:android开发

原文地址:http://blog.csdn.net/xufeng0991/article/details/44064081

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!