SharedPreferences保存用户偏好设置

使用 sharepreferences存储用户偏好设置和回显代码如下:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context="com.example.lin.sharepreference.MainActivity">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/name"/>
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/age"/>
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/age"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:onClick="save"/>
</LinearLayout>


MainActivity.java

package com.example.lin.sharepreference;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lin.service.PreferenceService;

import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private EditText nameText;
    private  EditText ageText;
    private   PreferenceService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameText=(EditText)this.findViewById(R.id.name);
        ageText= (EditText)this.findViewById(R.id.age);
        service=new PreferenceService(this);
        //回显示
        Map<String,String> params=service.getPreferences();
        nameText.setText(params.get("name"));
        ageText.setText(params.get("age"));
    }
    public void save(View v){
        String name=nameText.getText().toString();
        String age=ageText.getText().toString();
        service.save(name,Integer.valueOf(age));
        Toast.makeText(getApplicationContext(),R.string.success,Toast.LENGTH_SHORT).show();
    }
}


PreferenceService.java

package com.example.lin.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.EditText;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lin on 2016/6/21.
 */
public class PreferenceService {
    private Context context;
    public PreferenceService(Context context) {
        this.context = context;
    }

    /**
     * 保存用户数据
     * @param name 姓名
     * @param age 年龄
     */
    public void save(String name, Integer age) {
        SharedPreferences prefercences=context.getSharedPreferences("lin",Context.MODE_PRIVATE);//第一个参数代表名称,第二参数代表操作模式
        Editor editor=prefercences.edit();
        editor.putString("name",name);
        editor.putInt("age",age);
        editor.commit();
    }

    /**
     *获取各项配置参数
     * @return
     */
    public Map<String,String> getPreferences(){
        Map<String,String> params=new HashMap<String,String>();
        SharedPreferences prefercences=context.getSharedPreferences("lin",Context.MODE_PRIVATE);
        params.put("name",prefercences.getString("name",""));
        params.put("age",String.valueOf(prefercences.getInt("age",0)));
        return params;
    }
}
String.xml
<resources>
    <string name="app_name">软件参数设置</string>
    <string name="name">姓名</string>
    <string name="age">年龄</string>
    <string name="button">保存参数</string>
    <string name="success">保存成功</string>
</resources>
sharepreferences保存的数据路径:/data/data/package/shared_prefs/名称.xml



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 利用SharedPreferences可以方便地保存数据,它是Android中的一个轻量级存储类。下面是使用SharedPreferences保存数据的步骤: 1. 首先,获取SharedPreferences对象: SharedPreferences sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE); 这里的"data"是文件名,可以根据需要自定义。 2. 在SharedPreferences对象中,我们可以使用Editor对象来进行数据的编辑和保存SharedPreferences.Editor editor = sharedPreferences.edit(); 3. 使用Editor对象进行数据的编辑和保存,可以向其中添加键值对: editor.putString("name", "John"); // 保存字符串类型数据 editor.putInt("age", 25); // 保存整型数据 editor.putBoolean("isMale", true); // 保存布尔类型数据 在这里,"name"、"age"和"isMale"是键名,可以根据需求自定义。分别保存了字符串类型、整型和布尔类型的数据。 4. 完成数据的编辑后,需要通过commit()方法将数据保存SharedPreferences文件中: editor.commit(); 5. 当需要从SharedPreferences中读取数据时,可以通过键名获取保存的数据: String name = sharedPreferences.getString("name", ""); // 获取字符串类型数据,第二个参数是默认值 int age = sharedPreferences.getInt("age", 0); // 获取整型数据,第二个参数是默认值 boolean isMale = sharedPreferences.getBoolean("isMale", false); // 获取布尔类型数据,第二个参数是默认值 以上就是利用SharedPreferences保存数据的基本步骤。SharedPreferences可以用于保存用户的配置信息、用户登录状态等数据,方便快捷地进行读取和修改。 ### 回答2: SharedPreferences是一种用于Android开发的轻量级数据存储方式。它以键值对的形式来存储数据,并且数据可以被多个组件和应用程序共享使用。我们可以使用SharedPreferences保存一些简单的数据,比如用户偏好设置、登录状态、应用程序状态等。 首先,我们需要通过Context对象获取SharedPreferences对象。然后,我们可以使用SharedPreferences对象的edit()方法来获取一个Editor对象,通过Editor对象来写入数据。最后,我们要记得调用commit()方法提交数据的修改。 下面是一个保存数据到SharedPreferences的例子: ```java // 获取SharedPreferences对象 SharedPreferences sharedPreferences = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE); // 获取Editor对象 SharedPreferences.Editor editor = sharedPreferences.edit(); // 写入数据 editor.putString("username", "John"); editor.putInt("age", 25); editor.putBoolean("loggedIn", true); // 提交修改 editor.commit(); ``` 在这个例子中,我们创建了一个名为"MyPreferences"的SharedPreferences对象,并通过Editor对象对数据进行了写入操作。我们分别保存了一个用户名、一个年龄和一个登录状态。 我们可以使用getString()、getInt()、getBoolean()等方法来读取保存SharedPreferences中的数据。下面是一个读取数据的例子: ```java // 获取SharedPreferences对象 SharedPreferences sharedPreferences = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE); // 读取数据 String username = sharedPreferences.getString("username", ""); int age = sharedPreferences.getInt("age", 0); boolean loggedIn = sharedPreferences.getBoolean("loggedIn", false); ``` 在这个例子中,我们调用了getString()、getInt()、getBoolean()等方法来读取保存SharedPreferences中的数据,并将数据保存在对应的变量中。 总的来说,利用SharedPreferences保存数据是一种简单而方便的方式。它适用于存储一些简单的应用程序状态或用户偏好设置等数据,但如果需要存储大量的结构化数据,可能不太适合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值