您的位置:首页 > 移动开发 > Android开发

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>
        
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息