浅析ShardPreferences简单数据存储
来源:互联网 发布:java ojdbc6 编辑:程序博客网 时间:2024/06/06 16:35
SharedPreferences
1.是一种轻量级的数据存储方式
2.本质是基于XML文件存储key-value键值对数据
3.通常用来存储一些简单的配置信息
4.Shardpreferences对象本身只能获取数据而不支持存储和修改,存储和修改是通过Editor对象实现
5.实现ShardPreferences存储的步骤如下:
(1)获得ShardPreferences对象
(2)获得ShardPreferences.Editor对象
(3)通过Editor接口的putXxx方法保存key-value对其中Xxx表示不同的数据类型
(4)通过Editor接口的commmit方法保存key-value对象
记住用户名Demo
public class ShardPreActivity extends AppCompatActivity implements View.OnClickListener{ private EditText etName,etPass; private CheckBox checkBox; SharedPreferences pref; SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shard_pre); etName = (EditText) findViewById(R.id.editText); etPass = (EditText) findViewById(R.id.editText2); checkBox = (CheckBox) findViewById(R.id.checkBox); findViewById(R.id.button).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); pref = getSharedPreferences("UserInfo",MODE_PRIVATE); editor = pref.edit(); String name = pref.getString("userName",""); etName.setText(name); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button:{ String userName = etName.getText().toString().trim(); String userPass = etPass.getText().toString().trim(); if ("admin".equals(userName)&&"123".equals(userPass)){ if (checkBox.isChecked()){ editor.putString("userName",userName); editor.commit(); }else{ editor.remove("userName"); editor.commit(); } Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(this, "用户名或密码错误,登录失败", Toast.LENGTH_SHORT).show(); } } case R.id.button2:{ } } }} <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="17dp" android:layout_marginTop="36dp" android:text="用户名" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="19dp" android:text="密 码" android:layout_below="@+id/editText" android:layout_alignStart="@+id/textView" /> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="CheckBox" android:checked="false" android:layout_below="@+id/editText2" android:layout_alignStart="@+id/textView2" /> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/textView" android:layout_centerHorizontal="true" android:ems="10" android:inputType="textPersonName" android:text="Name" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/textView2" android:layout_centerHorizontal="true" android:ems="10" android:inputType="textPassword" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/checkBox" android:layout_below="@+id/checkBox" android:layout_marginStart="21dp" android:layout_marginTop="33dp" android:text="确定" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignEnd="@+id/editText2" android:layout_alignTop="@+id/button" android:layout_marginEnd="18dp" android:text="取消" /></RelativeLayout>
阅读全文
1 0
- 浅析ShardPreferences简单数据存储
- android中数据的存储方式(ShardPreferences)
- 浅析Android数据存储
- 数据存储-简单存储
- 数据存储之sqlite 浅析
- 浅析数据分层存储的又一层
- 浅析数据分层存储的又一层
- 浅析Android 程序数据存储及安全性
- 浮点型数据存储方式浅析
- ios-存储数据的方式浅析
- 保存简单的应用程序数据---SharedPreferences浅析
- ShardPreferences 保存登录状态
- Android简单数据存储SharedPreferences
- Python shelve简单数据存储
- Python shelve简单数据存储
- android 简单数据存储SharedPreferences
- Android简单数据存储SharedPreferences
- iOS_简单的数据存储
- Android getDeviceId
- Angularjs-(1)(简介、表达式、指令、控制器、过滤器)
- POMDP详解
- 入门kotlin(下)
- ImageLoader超级详细版
- 浅析ShardPreferences简单数据存储
- 数据库常见面试题
- VirtualBox5.1.24与CentOS-6.5网络设置
- 第3章软件也要拼脸的 3.4.1引入布局
- Hibernate Validator 校验方法的参数,返回值,构造函数
- 动态规划之单调队列
- 杭电 Euler theorem
- mac 下安装docker,在docker下安装oracle
- [ACL2017]Enhanced LSTM for Natural Language Inference