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

Android SharedPreferences的轻量级数据存储类的使用 记住密码小案例

2017-04-23 21:00 507 查看
今天做一个记住密码的小案例,这里用到了一个非常常用的一个存储类,SharedPreferences类。

首先,我们看一下是怎么保存信息的 。

private SharedPreferences sharedPreferences;
sharedPreferences =getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);//实例化sharedPreferences对象,将信息存在rememberpassword.xml文件中

SharedPreferences.Editor editor = sharedPreferences.edit();//开启编辑器

editor.putString("name",name); editor.putString("password",password);//name,password已经从TextView中获取了,存入数据

editor.commit();//最后提交一下。


然后,我们介绍一下怎么读取数据。

private SharedPreferences sharedPreferences;
sharedPreferences =getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);

String name =sharedPreferences.getString("name","");
String password = sharedPreferences.getString("password","");
//将name ,password就读取了


最后附上详细代码。

登录界面

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/gray"
tools:context="com.bzu.cyz.myapplication.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="20dp"
android:id="@+id/textView"
android:layout_alignBottom="@+id/et_user" />

<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/et_user"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码"
android:textSize="20dp"
android:layout_alignBottom="@+id/et_pass"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/textView2" />

<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/et_pass"
android:layout_below="@+id/et_user"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:password="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:onClick="login"
android:id="@+id/button"
android:layout_marginTop="77dp"
android:layout_below="@+id/et_pass"
android:layout_centerHorizontal="true"/>

<CheckBox
android:text="记住密码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cb1"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="33dp"/>
</RelativeLayout>


登录按钮设置一个点击事件login。一个很low的页面,大家如果想要好看的点的,可以自己加一下样式。

MainActivity

public class MainActivity extends AppCompatActivity {
private EditText eName;
private EditText ePassword;
private CheckBox checkBox;
private SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
sharedPreferences =getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);
boolean isremember =sharedPreferences.getBoolean("rememberpassword",false);
if(isremember){
String name =sharedPreferences.getString("name","");
String password = sharedPreferences.getString("password","");
eName.setText(name);
ePassword.setText(password);
checkBox.setChecked(true);

}

}

private void initView() {
eName = (EditText) findViewById(R.id.et_user);
ePassword = (EditText) findViewById(R.id.et_pass);
checkBox = (CheckBox) findViewById(R.id.cb1);

}
public  void login(View view){
String name = eName.getText().toString();
String password = ePassword.getText().toString();
if("admin".equals(name)&&"123".equals(password)){
SharedPreferences.Editor editor = sharedPreferences.edit();
if(checkBox.isChecked()){
editor.putBoolean("rememberpassword",true);
editor.putString("name",name);
editor.putString("password",password);
}else {
editor.clear();
}
editor.commit();
Intent intent = new Intent(this,WelcomeActivity.class);
startActivity(intent);
finish();
}else {
Toast.makeText(this,"密码账号错误",Toast.LENGTH_LONG).show();
}
}

}


多选框被选中时才会保存数据。还有一个跳转。随便写一个WelcomeActivity.java就好。

就这样,当选中记住密码点击登录跳转到另一个页面,数据就会保存到文件中,当关闭应用,再次打开时,会发现数据已经呈现在文本框中了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 数据存储
相关文章推荐