Android中使用SharedPreferences保存数据
来源:互联网 发布:网络通信工程师考试 编辑:程序博客网 时间:2024/05/18 20:46
SharedPreferences是Android平台提供的一个轻量级的存储类,存储格式是key-value(键值对),使用xml文件保存数据,文件存放在/data/data/package-name/shared_prefs目录下。
基本使用,可用于软件的基本设置,或者是保存用户的基本信息。
SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE); sharedPreferences.edit().putString("name", "wxq").commit(); sharedPreferences.edit().putString("age", "11").commit();
可在adb shell下/data/data/package-name/shared_prefs/目录下找到保存的数据文件
查看config.xml文件,可以看到保存的数据
3. 下面使用SharedPreferences保存登录用户名和密码(示例,密码不会这样保存)
输入用户名和密码后,点击保存,用户名和密码保存到/data/data/package-name/shared_prefs/目录下,当重新启动软件,点击读取,读取上次保存的数据。
直接贴代码了。。。
布局文件
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/name_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" /> <EditText android:id="@+id/pass_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="请输入密码" android:inputType="numberPassword" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:orientation="horizontal"> <Button android:id="@+id/save_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="保存" /> <Button android:id="@+id/read_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="读取" /> </LinearLayout></LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { private EditText nameEt, passEt; private Button saveBtn, readBtn; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE); initVies(); initListener(); } private void initVies() { nameEt = (EditText) findViewById(R.id.name_edit); passEt = (EditText) findViewById(R.id.pass_edit); saveBtn = (Button) findViewById(R.id.save_btn); readBtn = (Button) findViewById(R.id.read_btn); } private void initListener() { saveBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!TextUtils.isEmpty(nameEt.getText().toString())) { sharedPreferences.edit() .putString("name", nameEt.getText().toString()) .commit(); } if (!TextUtils.isEmpty(passEt.getText().toString())) { sharedPreferences.edit() .putString("pass", passEt.getText().toString()) .commit(); } } }); readBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { nameEt.setText(sharedPreferences.getString("name", "")); passEt.setText(sharedPreferences.getString("pass", "")); } }); }}
点击保存按钮,获取用户名和密码保存到config.xml文件中。
点击读取按钮,获取config.xml文件中的用户名和密码显示到控件上。
0 0
- Android中使用SharedPreferences保存数据
- 在Android中使用SharedPreferences保存简单的数据
- android中使用SharedPreferences保存数据丢失的问题
- Android使用SharedPreferences保存数据实例
- Android --使用SharedPreferences保存和获取数据
- android 使用SharedPreferences保存list数据
- Android开发---使用SharedPreferences保存数据
- fragment中使用sharedPreferences保存数据。
- Android 保存数据SharedPreferences
- SharedPreferences保存android数据
- android SharedPreferences保存数据
- 使用SharedPreferences保存数据
- android SharedPreferences保存应用数据
- Android采用SharedPreferences保存数据
- Android采用SharedPreferences保存数据
- android sharedpreferences保存数据注意事项
- Android采用SharedPreferences保存数据
- Android数据保存之SharedPreferences
- 401. Binary Watch
- DBUtil
- python 字符串创建,遍历,切片,格式化ljust rjust center,find,replace,split,repr,str
- 多线程概述及简单用法
- 蓝桥杯练习系统基础练习——十进制转十六进制
- Android中使用SharedPreferences保存数据
- 微信公众号开发10数据库修改
- SpringMVC详细示例实战教程
- Java EE Web开发系列导航
- DBUtils 框架的中batch批处理使用
- Maven新手介绍如何使用Eclipse创建Maven Web工程的方法
- linux deb包的安装
- Android设计模式---适配器模式
- 事务