SharedPreferences 的使用
来源:互联网 发布:淘宝网官方版下载安装 编辑:程序博客网 时间:2024/06/07 02:31
一.存储方式
SharedPreferences 中存储的数据是以键值对的形式保存在XML文件中。
二.获取实例对象的方法
1.通过context.getSharedPreferences(Stringname,int mode)获取
2.Activity中的getPreferences()方法
3.PrefencesManager类中的getDefauHSharedPerferences()获取
三.实例---登录
Login—Top
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin"
android:background="#E6FF"
android:weightSum="1">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EDFF"
android:drawableLeft="@drawable/icon_user"
android:drawablePadding="10dp"
android:ems="10"
android:hint="请输入账号">
<requestFocus />
</EditText>
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etName"
android:layout_marginTop="5dp"
android:background="#EDFF"
android:drawableLeft="@drawable/icon_pass"
android:drawablePadding="10dp"
android:ems="10"
android:hint="请输入密码"
android:inputType="textPassword">
<requestFocus />
</EditText>
<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/ctIsRememberPass"
android:textSize="20sp"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#003D79"
android:onClick="login"
android:text="登录"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/deer"
android:id="@+id/imageView"
android:layout_weight="0.78" />
</LinearLayout>
LoginActivity.java
package cn.edu.bzu.myapplication;
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 isChecked;
private CheckBox cbIsRememberPass;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initViews();
sharedPreferences=getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);
Boolean isRemember= sharedPreferences.getBoolean(“remremberpassword”,false);
If(isRemember){
String name= sharedPreferences.getString(“name”,””);
String password= sharedPreferences.getString(“password”,””);
etName.setText(name);
etPassword.setText(password);
cbIsRememberPass.setCheck(true);
}
}
private 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.MainActivity.class);
startActivity(Intent);
finish();
}else{
Toast.makeText(this.”账号或密码有误”);
}
}
}
MainActivity
- SharedPreferences的使用
- SharedPreferences的使用
- SharedPreferences的使用
- SharedPreferences的使用
- SharedPreferences的使用
- android SharedPreferences 的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- SharedPreferences的使用
- Android SharedPreferences的使用
- android SharedPreferences的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- android SharedPreferences 的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- ubuntu install Java jdk8
- C#对于md5加密的两种方式
- Bootstrap常用表单组件
- unity发布apk如何配置环境
- CentOS和RHEL使用源码包安装git
- SharedPreferences 的使用
- Android Xposed框架 -- 应用篇
- C++第三次作业报告
- 2016 版 Laravel 系列入门教程(一)【最适合中国人的 Laravel 教程】
- #OSG+VS#第七周 关于osgearth中shadowing的理解
- Java实现字符串ID自增
- eval 在matlab中的用法
- 定时任务执行脚本文件
- 学习C语言的教材