Android学习(5)——小案例(SharedPreference)
来源:互联网 发布:python 最优化算法包 编辑:程序博客网 时间:2024/06/08 19:49
SharedPreferences是Android平台上一个轻量级的存储器,主要用于存储一些应用程序的配置参数。SharedPreferences中存储的数据是以key/value键值对的形式保存在xml文件中,该文件位于data/data//shared_prefs文件夹中。
注意:SharedPreferences中的value值只能是float、int、long、boolean、String、StringSet类型数据。
案例实现:
当用户选中了记住密码复选框,并成功登录一次之后,这个时候如果再重新启动登录界面,之前输入的用户名和密码就会显示在文本框中。
图片素材:
ActivityMain.xml
<?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="#A4D3EE" tools:context="com.example.administrator.login.LoginActivity"> <include layout="@layout/login_top"></include> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/deer" android:id="@+id/imageView" android:layout_alignParentBottom="true" /></RelativeLayout>
ActivityTwo.xml
?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_two" 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="com.example.administrator.login.TwoActivity"> <TextView android:text="Welcome" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:textSize="40sp" android:id="@+id/textView" /></RelativeLayout>
LoginTop.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="32dp"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/etName" android:drawableLeft="@drawable/icon_user" android:drawablePadding="10dp" android:ems="10" android:hint="请输入账号" android:background="#ffffff"> <requestFocus/> </EditText> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/etPassword" android:layout_below="@id/etName" android:drawableLeft="@drawable/icon_pass" android:drawablePadding="10dp" android:ems="10" android:hint="请输入密码" android:background="#ffffff"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/etPassword"> <CheckBox android:text="记住密码" android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/cbIsRememberPass" android:textSize="20sp" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#6495ED" android:onClick="login" android:text="登录"/> </LinearLayout></RelativeLayout>
LoginActivity.java
package com.example.administrator.login;import android.content.Context;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;public class LoginActivity extends AppCompatActivity { private EditText etName; private EditText etPassword; private CheckBox cbIsRememberPass; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); sharedPreferences=getSharedPreferences("rememberpassword",Context.MODE_PRIVATE); boolean isRemember= sharedPreferences.getBoolean("rememberpassword",false); if(isRemember){ String name=sharedPreferences.getString("name",""); String password=sharedPreferences.getString("password",""); etName.setText(name); etPassword.setText(password); cbIsRememberPass.setChecked(true); } } public void initViews(){ etName=(EditText)findViewById(R.id.etName); etPassword=(EditText)findViewById(R.id.etPassword); cbIsRememberPass=(CheckBox)findViewById(R.id.cbIsRememberPass); } public void login(View view){ String name=etName.getText().toString(); String password=etPassword.getText().toString(); if("admin".equals(name)&&"123456".equals(password)){ SharedPreferences.Editor editor=sharedPreferences.edit(); if(cbIsRememberPass.isChecked()){ editor.putBoolean("rememberpassword",true); editor.putString("name",name); editor.putString("password",password); }else{ editor.clear(); } editor.commit();Intent intent=new Intent(this,TwoActivity.class);startActivity(intent);finish(); }else { Toast.makeText(this,"账号或密码错误",Toast.LENGTH_LONG);} }}
效果图片
输入信息
进入页面
实现小案例。
0 0
- Android学习(5)——小案例(SharedPreference)
- Android学习小Demo(7)SharedPreference的使用
- Android学习笔记——sharedPreference
- android开发学习(三)——sharedPreference,xml文件序列化和反序列
- River的Android学习笔记——数据持久化技术(SharedPreference存储)
- Android存储——SharedPreference存储(用户偏好存储)
- Android首选项SharedPreference-android学习之旅(六)
- Android学习13——数据存储之SharedPreference
- android学习—数据存储和IO-SharedPreference和Editor
- android学习—数据存储和IO-SharedPreference和Editor
- Android学习笔记033之数据存储—SharedPreference
- Android学习笔记三十三之数据存储—SharedPreference
- Android学习(11)-文件和SharedPreference存储
- android数据存储—SharedPreference
- Android学习第一天.(电话拨号器小案例)
- Android Sharedpreference学习
- 【Android】通用系列 —— 数据持久化(通用的SharedPreference封装)
- Android 数据存储——SharedPreference
- DeepID人脸识别算法之三代
- 杨辉三角
- 程序员面试经典--汉诺塔
- android 仿Listview增删数据
- Rikka with Graph(水题)
- Android学习(5)——小案例(SharedPreference)
- 基于MCS-51内核的汇编操作符笔记
- 数组中两个数异或求最大值
- JAVA接口
- 生成xml
- 真正的大神
- 一、SpringBoot精要
- Java面试题
- linux命令学习4