android studio关于sharedPreferences的数据存储
来源:互联网 发布:淘宝客app制作 编辑:程序博客网 时间:2024/06/06 23:51
本人写这篇博客是为了复习之前学习的sharedPreferences的存储方法:本人刚学android所以如果我说的哪里有错请位见谅并指出谢谢!
如果是Android应用,Android 平台提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,
文件存放在/data/data//shared_prefs目录下。(当然我也没有找到这个目录下的文件,我之前以为这个文件是需要创建的结果是已经存在的所以我就没有去找,继续写我的代码了,当然我这种行为是不可取的)。
我坐的例子是用户登录之后存储用户的账号及密码并在第二个Activity中打印出来这些信息。
接下来是代码部分:
如果是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.javapublic 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); }}
0 0
- android studio关于sharedPreferences的数据存储
- android的数据存储 SharedPreferences
- Android的数据存储--SharedPreferences .
- Android的数据存储--SharedPreferences
- Android的数据存储--SharedPreferences
- Android的数据存储--SharedPreferences
- Android SharedPreferences的数据存储
- android studio for android learning (十四) android的数据的存储sharedPreferences
- Android之Android的数据存储--SharedPreferences
- Android数据存储-SharedPreferences
- Android数据存储---SharedPreferences
- Android数据存储---SharedPreferences
- android SharedPreferences数据存储
- Android 数据存储 -- SharedPreferences
- android SharedPreferences 数据存储
- android数据存储---SharedPreferences
- Android 数据存储SharedPreferences
- android数据存储SharedPreferences
- Linux——调试中提示信息:bash: groupadd: command not found
- 利用辗转相除求两个数的最大公约数
- HDU 1237 简单计算器(栈的应用)
- 洛谷 1041
- ubuntu配置jdk环境
- android studio关于sharedPreferences的数据存储
- 第八周OJ4小球自由下落
- LeetCode 117 Populating Next Right Pointers in Each Node II (链表 层次遍历 推荐)
- JSON格式数据从servlet传输至前台具体方法
- 【fzyzoj1578】数列分段
- 87. Scramble String
- java--jstl小结(MVC框架2)
- SAP(成都站)面试经验
- spring+dubbo+zookeeper整合