android studio关于sharedPreferences的数据存储
2016-10-25 19:42
239 查看
本人写这篇博客是为了复习之前学习的sharedPreferences的存储方法:本人刚学android所以如果我说的哪里有错请位见谅并指出谢谢!
如果是Android应用,Android 平台提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,
文件存放在/data/data//shared_prefs目录下。(当然我也没有找到这个目录下的文件,我之前以为这个文件是需要创建的结果是已经存在的所以我就没有去找,继续写我的代码了,当然我这种行为是不可取的)。
我坐的例子是用户登录之后存储用户的账号及密码并在第二个Activity中打印出来这些信息。
接下来是代码部分:
activity_main.xml文件的代码
<RelativeLayout 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"
tools:context="com.example.jzc.ex_sharedpreferences.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50sp"
android:layout_marginLeft="30sp"
android:text="账号:"
android:textSize="20sp"/>
<EditText
android:id="@+id/Ed_UserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40sp"
android:layout_marginLeft="90sp"
android:ems="10"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:id="@+id/textView"
android:layout_marginLeft="30sp"
android:layout_marginTop="120sp"
android:textSize="20sp"/>
<EditText
android:id="@+id/Ed_Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="110sp"
android:layout_marginLeft="90sp"
android:inputType="textPassword"
android:ems="10"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80sp"
android:layout_marginTop="220sp"
android:text="登录"
android:id="@+id/btn_login" />
</RelativeLayout>
activity_main2.xml文件的代码<RelativeLayout 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"
tools:context="com.example.jzc.ex_sharedpreferences.Main2Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80sp"
android:layout_marginLeft="30sp"
android:text="账号:"
android:textSize="40px"/>
<TextView
android:id="@+id/txt_UserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80sp"
android:layout_marginLeft="90sp"
android:textSize="40px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120sp"
android:layout_marginLeft="30sp"
android:text="密码:"
android:textSize="40px"/>
<TextView
android:id="@+id/txt_Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120sp"
android:layout_marginLeft="90sp"
android:textSize="40px"/>
</RelativeLayout>
如果是Android应用,Android 平台提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,
文件存放在/data/data//shared_prefs目录下。(当然我也没有找到这个目录下的文件,我之前以为这个文件是需要创建的结果是已经存在的所以我就没有去找,继续写我的代码了,当然我这种行为是不可取的)。
我坐的例子是用户登录之后存储用户的账号及密码并在第二个Activity中打印出来这些信息。
接下来是代码部分:
activity_main.xml文件的代码
<RelativeLayout 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"
tools:context="com.example.jzc.ex_sharedpreferences.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50sp"
android:layout_marginLeft="30sp"
android:text="账号:"
android:textSize="20sp"/>
<EditText
android:id="@+id/Ed_UserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40sp"
android:layout_marginLeft="90sp"
android:ems="10"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:id="@+id/textView"
android:layout_marginLeft="30sp"
android:layout_marginTop="120sp"
android:textSize="20sp"/>
<EditText
android:id="@+id/Ed_Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="110sp"
android:layout_marginLeft="90sp"
android:inputType="textPassword"
android:ems="10"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80sp"
android:layout_marginTop="220sp"
android:text="登录"
android:id="@+id/btn_login" />
</RelativeLayout>
activity_main2.xml文件的代码<RelativeLayout 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"
tools:context="com.example.jzc.ex_sharedpreferences.Main2Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80sp"
android:layout_marginLeft="30sp"
android:text="账号:"
android:textSize="40px"/>
<TextView
android:id="@+id/txt_UserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80sp"
android:layout_marginLeft="90sp"
android:textSize="40px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120sp"
android:layout_marginLeft="30sp"
android:text="密码:"
android:textSize="40px"/>
<TextView
android:id="@+id/txt_Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120sp"
android:layout_marginLeft="90sp"
android:textSize="40px"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件 Button btnLogin=(Button)findViewById(R.id.btn_login); final EditText EdPassword=(EditText) findViewById(R.id.Ed_Password); final EditText EdUserName=(EditText)findViewById(R.id.Ed_UserName); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences sharedPreferences = getSharedPreferences("jzc",Context.MODE_PRIVATE);//创建SharedPreferences对象 SharedPreferences.Editor editor = sharedPreferences.edit(); //需要获取SharedPreferences的编辑对象 editor.putString("username", EdUserName.getText().toString()); //向preferences写入数据: editor.putString("Password", EdPassword.getText().toString()); editor.commit();// 向preferences文件中提交数据: Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } }); } }
MainActivity2.java
public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); TextView txtUserName=(TextView) findViewById(R.id.txt_UserName); TextView txtPassword=(TextView) findViewById(R.id.txt_Password); SharedPreferences sharedPreferences = getSharedPreferences("jzc", Context.MODE_PRIVATE); //创建SharedPreferences对象 String userName=sharedPreferences.getString("username","");//根据key获取对应的数据 String password=sharedPreferences.getString("Password",""); txtPassword.setText(password);//给TextView设置text的值 txtUserName.setText(userName); } }
相关文章推荐
- 关于自定义实体集合绑定datagrid控件在进行编辑时出现错误"向原始数据存储区提交行时出错"的原因.
- 关于用VC,VB进行图像数据(二进制大对象)存储数据库的一点心得
- 手把手教你开发Chrome扩展三:关于本地存储数据
- java笔记:关于复杂数据存储的问题--基础篇:数组以及浅拷贝与深拷贝的问题(上)
- 企业数据安全:关于数据存储的五个事实
- 关于VB中数据的存储格式和寻址方式
- 一些关于数据存储和查询优化的想法
- 关于添加索引视图后的数据存储区别
- 关于keyValueStore与普通数据存储之间的一种映射模型
- java笔记:关于复杂数据存储的问题--基础篇:数组以及浅拷贝与深拷贝的问题(上)
- 近期整理的关于数据存储和备份的一些资料
- ABAP--关于存储全局abap类的信息数据表
- ABAP--关于存储全局abap类的信息数据表
- Python 关于python的数据存储方面
- 关于数据存储的一些琐事
- 关于在BSTR中存储二进制数据的若干问题
- ABAP--关于存储全局abap类的信息数据表
- 留个记念,第一次写这么长的存储过程。关于台湾地址excel中数据从一张表中导入另一张不同结构的表!
- 关于数据存储的五个事实
- 一个关于继承的程序,求高人解释程序执行过程中内存的数据存储情况