星空网 > 软件开发 > 操作系统

android开发之路11(用SharedPreferences存储数据)

Android平台给我们提供了一个SharedPreferences类,实际上SharedPreferences处理的就是一个key-value(键值对),它是

 

一个轻量级的存储类,特别适合用于保存软件配置参数及用户的偏好设置参数,比如登录时候的记住密码功能等。使用

 

SharedPreferences保存数据,实际上是用文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下

 

1.获取SharedPreferences对象的两种方式:

①调用Context对象的getSharedPreferences()方法

②调用Activity对象的getPreferences()方法

两种方式的区别:

调用Context对象的getSharedPreferences()方法获得的SharedPreferences对象可以被同一应用程序下的其他组件共享.

调用Activity对象的getPreferences()方法获得的SharedPreferences对象只能在该Activity中使用.

2.SharedPreferences的四种操作模式:

Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件

 

的内容

Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.

Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.

MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.

MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入.

 

3.SharedPreferences类的应用实例:

①创建布局文件:activity_main.

<LinearLayout 

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/name" />

    <EditText 

        android:id="@+id/name"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"/>

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/age" />

    <EditText 

        android:id="@+id/age"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:numeric="integer"/>

    <!--android:onClick用于指定一个方法名称,按钮被点击后就会执行该方法  -->

    <Button 

        android:id="@+id/button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/button"

        android:onClick="save"/>

 

</LinearLayout>

 

②创建业务类PreferenceService.java

public class PreferenceService {

private Context context;

 

public PreferenceService(Context context) {

this.context = context;

}

 

//保存配置参数

public void save(String name,Integer age){

SharedPreferences sp=context.getSharedPreferences("testSP", Context.MODE_PRIVATE);

Editor editor=sp.edit();

editor.putString("name", name);

editor.putInt("age", age);

//将数据提交的文件中

editor.commit();

}

//获取配置参数

public Map<String, String> getPreference(){

//创建Map集合用来保存我们从SharedPreference中获取的数据

Map<String, String> params=new HashMap<String, String>();

SharedPreferences sp=context.getSharedPreferences("testSP", Context.MODE_PRIVATE);

//SharedPreferences类的getString("name", "")方法中第一个参数是参数名,第一个参数是参数的默认值

params.put("name", sp.getString("name", ""));

params.put("age", String.valueOf(sp.getInt("age", 0)));

return params;

 

}

 

}

 

③创建程序的入口MainActivity.java

public class MainActivity extends Activity {

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) findViewById(R.id.name);

        ageText=(EditText) findViewById(R.id.age);

        service =new PreferenceService(this);

        Map<String, String> params=service.getPreference();

        nameText.setText(params.get("name"));

        ageText.setText(params.get("age"));

        

    }

    /**

     * save方法要求:

     * 参数必须是View类型

     * 且无返回值

     */

    public void save(View v){

     String name=nameText.getText().toString();

     String age=ageText.getText().toString();

     service.save(name,Integer.valueOf(age));

     Toast.makeText(getApplicationContext(),"保存成功", Toast.LENGTH_LONG).show();

    }

}

 

 

 




原标题:android开发之路11(用SharedPreferences存储数据)

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

传统货代:https://www.goluckyvip.com/tag/4195.html
如何爆单:https://www.goluckyvip.com/tag/4197.html
新选品计划:https://www.goluckyvip.com/tag/4198.html
产品卖点:https://www.goluckyvip.com/tag/4199.html
支付监管:https://www.goluckyvip.com/tag/42.html
香港交通瘫痪:https://www.goluckyvip.com/tag/420.html
​TikTok发生了什么?美区增长首次停滞,准入管理力度加大:https://www.kjdsnews.com/a/1836407.html
2024年如何找到在TikTok上发帖的最佳时间:https://www.kjdsnews.com/a/1836408.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流