安卓SharedPrefences详解以及记住密码的小例子
来源:互联网 发布:网络延长器多少钱 编辑:程序博客网 时间:2024/05/22 02:12
1.Sharedfrefences的作用
SharedPreferences是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。
存储在在/data/data/<包名>/shared_prefs目录下。
2.用SharedPrefences存储数据
Context context = MainActivity.this;
SharedPreferences sharedPreferences = context.getSharedPreferences("MY_PRE", MODE_PRIVATE);//执行完成后会在/data/data/<包名>/shared_prefs目录下生成一个MY_PRE.xml文件
Editor editor = sharedPreferences.edit();//需要用Editor
editor.putString("user", userString);
editor.putString("password", passwordString);
editor.commit();//记得要commit一下
editor.putString("password", passwordString);
editor.commit();//记得要commit一下
3.从SharedPrefences中取数据
用sharedPreferences.getString("user", "")取出key为"user"的值,如果该key不存在,则将该key的值赋为“”
4.一个登陆的记住账号密码的小例子
activity_mail.xml
<span style="font-family:Microsoft YaHei;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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=".MainActivity" > <LinearLayout android:id="@+id/lin1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="6px" android:orientation="horizontal"> <TextView android:id="@+id/user" android:layout_width="150px" android:layout_height="wrap_content" android:text="user"/> <EditText android:id="@+id/myuser" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:id="@+id/lin2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="6px" android:orientation="horizontal" android:layout_below="@id/lin1"> <TextView android:id="@+id/password" android:layout_width="150px" android:layout_height="wrap_content" android:text="password"/> <EditText android:id="@+id/mypassword" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> <Button android:id="@+id/login" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Login" android:layout_below="@id/lin2"/> <LinearLayout android:id="@+id/lin3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="6px" android:orientation="horizontal" android:layout_below="@id/login"> <CheckBox android:id="@+id/remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="记住密码?"/> </LinearLayout> </RelativeLayout></span>
MainAcitvity----如果用户名是zyr,密码是zyr123,则跳到BActivity
<span style="font-family:Microsoft YaHei;">package com.example.sharepreferencestest;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener,OnCheckedChangeListener{private EditText userEditText;private EditText passwordEditText;private Button loginButton;private CheckBox rCheckBox;private String userString;private String passwordString;private SharedPreferences sharedPreferences;private Editor editor;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);userEditText = (EditText)findViewById(R.id.myuser);passwordEditText = (EditText)findViewById(R.id.mypassword);loginButton = (Button)findViewById(R.id.login);rCheckBox = (CheckBox)findViewById(R.id.remember);loginButton.setOnClickListener(this);rCheckBox.setOnCheckedChangeListener(this);//获取SharedPreferences对象Context context = MainActivity.this;sharedPreferences = context.getSharedPreferences("MY_PRE", MODE_PRIVATE);editor = sharedPreferences.edit();//userEditText.setText(sharedPreferences.getString("user", ""));passwordEditText.setText(sharedPreferences.getString("password", ""));}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.login:userString = userEditText.getText().toString();passwordString = passwordEditText.getText().toString();//如果记住用户密码的话,在点击登录按钮时,写数据if(rCheckBox.isChecked()){editor.putString("user", userString);editor.putString("password", passwordString);editor.commit();}if(userString.equals("zyr")&&passwordString.equals("zyr123")){Intent intent = new Intent();intent.setClass(MainActivity.this, BActivity.class);startActivity(intent);}break;default:break;}}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubswitch (buttonView.getId()) {case R.id.remember:System.out.println(isChecked);if(isChecked){//Toast.makeText(this, "checked", Toast.LENGTH_SHORT).show();}break;default:break;}}}</span>BActivity
<span style="font-family:Microsoft YaHei;">package com.example.sharepreferencestest;import android.app.Activity;import android.os.Bundle;public class BActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.b);}}</span>
b.xml
<span style="font-family:Microsoft YaHei;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/btext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="welcom!" android:gravity="center" android:textSize="30sp"/> </LinearLayout></span>
0 0
- 安卓SharedPrefences详解以及记住密码的小例子
- 安卓记住密码自动登录的实现
- 安卓记住密码和自动登录
- 安卓记住密码和自动登录
- 安卓开发点击CheckBox记住密码
- 安卓实现显示记住密码
- 小白---VSS 的记住密码
- Android中SharedPreferences存储详解(例子:记住密码)
- 安卓Shareprefrence实现记住密码自动登录以及保存在shareprefrence里面数据如何加密解密
- 安卓登陆框记住密码和自动登录的实现
- 安卓登陆框记住密码和自动登录的实现
- 安卓登陆框记住密码和自动登录的实现
- 安卓登陆框记住密码和自动登录的实现
- 安卓登陆矿记住密码和自动登录的实现
- 安卓登陆框记住密码和自动登陆的实现
- 安卓登陆框记住密码和自动登录的实现
- 安卓登陆框记住密码和自动登陆的实现
- 安卓登陆框记住密码和自动登录的实现 .
- ubuntu 14.04lts 安装Cisco Anyconnect Secure Mobility Client
- Scrapy入门
- Python异常处理总结
- uniDAC用法总结
- 知识回顾
- 安卓SharedPrefences详解以及记住密码的小例子
- 文件上传路径问题
- jQuery.Autocomplete实现自动完成功能(详解)
- C/C++ 内存检测工具Visual Leak Detector
- android自定义圆形ImageView(学习笔记整理)
- activemq-5.10本地运行demo
- 病毒木马查杀实战第012篇:QQ盗号木马之逆向分析
- viewpager 和水平scrollView 进行绑定。
- tokudb 个人整理