SharedPrefereces的存储方式
来源:互联网 发布:互动投影软件1.1 编辑:程序博客网 时间:2024/04/30 12:08
一.实验要求
当用户输入正确的用户名以及密码时,可以正确登录到welcome页面,当用户输入账户或密码时错误,则会吐出一个吐司,提示您输入的账号或密码错误,另外,当用户输入正确的账号和密码时,然后勾选“记住密码”选项时,会自动的保存用户输入的账号和密码,下一次用户登录时,可以直接登录,不用再输入账号和密码。但是,当用户输入的账号或密码错误时,不会保存输入的账号或密码,下一次登录时需要重新输入。
二.页面设置
1.主界面代码如下:
<?xml version="1.0" encoding="utf-8"?>
<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:background="@android:color/holo_blue_bright"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.edu.bzu.sharedpreferences.MainActivity">
<EditText
android:id="@+id/etuser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/name"
android:ems="10" />
<EditText
android:id="@+id/etpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/etuser"
android:background="@android:drawable/edit_text"
android:ems="15"
android:hint="密码:" />
<LinearLayout
android:id="@+id/l1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/etpassword"
android:orientation="horizontal">
<CheckBox
android:id="@+id/che"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.73"
android:text="记住密码"
android:textSize="20sp" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_weight="2"
android:onClick="login"
android:text="登录" />
</LinearLayout>
</RelativeLayout>
2.welcome界面代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_welcome"
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"
tools:context="cn.edu.bzu.sharedpreferences.welcome">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welcome you"
android:layout_centerInParent="true"
android:textSize="50sp"
/>
</RelativeLayout>
3.在MainActivity中的代码如下:
package cn.edu.bzu.sharedpreferences;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText etuser;
private EditText etpassword;
private CheckBox che;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etuser=(EditText)findViewById(R.id.etuser);
etpassword=(EditText)findViewById(R.id.etpassword);
che=(CheckBox)findViewById(R.id.che);
sharedPreferences =getSharedPreferences("loginRemember",MODE_PRIVATE);
boolean isRemember=sharedPreferences.getBoolean("loginRemember",false);
if(isRemember){
String name=sharedPreferences.getString("name","");
String password=sharedPreferences.getString("password","");
etuser.setText(name);
etpassword.setText(password);
che.setChecked(true);
}
}
public void login(View view) {
String name=etuser.getText().toString();
String password=etpassword.getText().toString();
if("admin".equals(name)&&"123456".equals(password)){
SharedPreferences.Editor editor=sharedPreferences.edit();
if(che.isChecked()){
editor.putBoolean("loginRemember",true);
editor.putString("name",name);
editor.putString("password",password);
}
else
{
editor.clear();
}
editor.commit();
Intent intent=new Intent(this,welcome.class);
startActivity(intent);
finish();
}
else{
Toast.makeText(this,"您输入的用户名或密码错误",Toast.LENGTH_LONG).show();
}
}
}
如此就可以实现上述所说的功能啦!!
- SharedPrefereces的存储方式
- SharedPrefereces的工具类
- sharedPrefereces
- Android——数据存储(四种方式之一)SharedPrefereces
- 比SharedPrefereces更强大的缓存工具类
- 存储单元的存储方式
- Session的存储方式
- x264_scan8 的存储方式
- 图的存储方式
- session的存储方式
- 图的存储方式
- Android的存储方式
- 图的存储方式
- HashMap 的存储方式
- 内存的存储方式
- 内存的存储方式
- Map的存储方式
- 图的存储方式
- 1022. Digital Library (30)
- linux 入门
- 4 输入输出管理 vim gedit.
- android学习(八) 使用系统权限
- python脚本出错:IndentationError: unindent does not match any outer indentation level
- SharedPrefereces的存储方式
- 怎样在excel中拉出递增的一列数
- Java中的算数运算
- tensorboard 在cmd下运行命令
- gcc总结
- centos7 配置jdk替换openjdk(下)
- u-boot-2016.09移植(8)-合并tq210-spl.bin与u-boot.bin
- SQL—游标
- jQuery 简单特效之 ---- jQuery 实现包含淡入效果的简单图片库