Android SharedPreferences的轻量级数据存储类的使用 记住密码小案例
2017-04-23 21:00
507 查看
今天做一个记住密码的小案例,这里用到了一个非常常用的一个存储类,SharedPreferences类。
首先,我们看一下是怎么保存信息的 。
然后,我们介绍一下怎么读取数据。
最后附上详细代码。
登录界面
登录按钮设置一个点击事件login。一个很low的页面,大家如果想要好看的点的,可以自己加一下样式。
MainActivity
多选框被选中时才会保存数据。还有一个跳转。随便写一个WelcomeActivity.java就好。
就这样,当选中记住密码点击登录跳转到另一个页面,数据就会保存到文件中,当关闭应用,再次打开时,会发现数据已经呈现在文本框中了。
首先,我们看一下是怎么保存信息的 。
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数据存储——登陆案例(记住密码)
- Android简单数据存储类SharedPreferences详解及实例(通过“记住密码”功能学习SharedPreferences)
- Android通过"记住密码"功能学习数据存储类SharedPreferences详解及实例
- Android数据存储和访问之SharedPreferences存储方式_用户登录记住密码案例
- Android简单数据存储类SharedPreferences详解及实例(通过“记住密码”功能学习SharedPreferences)
- Android简单数据存储类SharedPreferences详解及实例(通过“记住密码”功能学习SharedPreferences) .
- Android简单数据存储类SharedPreferences详解及实例(通过“记住密码”功能学习SharedPreferences)
- Android简单数据存储类SharedPreferences详解及实例(通过“记住密码”功能学习SharedPreferences)
- Android数据存储之SharedPreferences的简单使用(保存用户名和密码)
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- android轻量级缓存框架ASimpleCache的使用 (网络请求数据并缓存)
- Android 数据存储和文件使用案例分析
- Android中SharedPreferences的使用(登陆,记住密码)
- Android 手机存储及sd卡操作--案例登录记住帐号密码
- android SharedPreference的简单使用(登陆界面记住密码)
- Android中Sharedpreference的使用,记住登录后记住用户名密码
- Android中Json数据解析,Thread+Handler ,SimpleAdapter使用小案例
- Android 数据存储和文件使用案例分析