- 浏览: 283066 次
- 性别:
- 来自: 青岛
文章分类
最新评论
-
totot:
打扰大师了,acquireWakeLock(),releas ...
使用WakeLock使Android应用程序保持后台唤醒 -
totot:
打扰大师了,acquireWakeLock(),releas ...
使用WakeLock使Android应用程序保持后台唤醒 -
aaa6287152:
感谢楼主 问题解决 一开始按照第一条修改了,结果看第二条没有修 ...
The method of type must override a superclass method解决方式 -
Mark_dev:
chen646531623 写道达哥,很不幸,我出现了你所说的 ...
Unable to resolve host "...": No address associated 错误 解决方案 -
Mark_dev:
我也遇到楼上的那个问题。
Unable to resolve host "...": No address associated 错误 解决方案
SharedPreferences是Android中存储简单数据的一个工具类。可以想象它是一个小小的Cookie,它通过用键值对的 方式把简单数据类型(boolean、int、float、long和String)存储在应用程序的私有目录下(data/data/包名 /shared_prefs/)自己定义的xml文件中。
一、简介
它提供一种轻量级的数据存储方式,通过eidt()方法来修改里面的内容,通过Commit()方法来提交修改后的内容。
二、重要方法
public abstract boolean contains (String key) :检查是否已存在该文件,其中key是xml的文件名。
edit ():为preferences创建一个编辑器Editor,通过创建的Editor可以修改preferences里面的数据,但必须执行commit()方法。
getAll ():返回preferences里面的多有数据。
getBoolean (String key, boolean defValue):获取Boolean型数据
getFloat (String key, float defValue):获取Float型数据
getInt (String key, int defValue):获取Int型数据
getLong (String key, long defValue):获取Long型数据
getString (String key, String defValue):获取String型数据
registerOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener):注册一个当 preference发生改变时被调用的回调函数。
unregisterOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener):删除当前回调函数。
三、重要接口SharedPreferences.Editor
1.简介
用于修改SharedPreferences对象的内容,所有更改都是在编辑器所做的批处理,而不是复制回原来的SharedPreferences或持久化存储,直到你调用commit(),才将持久化存储。
2.重要方法
clear ():清除内容。
commit ():提交修改
remove (String key):删除preference
下面通过“记住密码”功能
四、实例
效果图如下
首页
登录成功后的页面
当第一次登录点击”记住密码“后,第二次打开时的页面
2.代码
布局文件 login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="right" android:layout_gravity="right"
android:background="@drawable/default_bg" android:orientation="vertical">
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="1">
<TableRow android:gravity="center" android:layout_gravity="center">
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/ivlogo"
>
</ImageView>
</TableRow>
</TableLayout>
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="1">
<TableRow android:layout_marginTop="100dip">
<TextView android:layout_width="wrap_content"
android:layout_marginLeft="20dip" android:gravity="center_vertical"
android:layout_height="wrap_content" android:id="@+id/tvaccount"
android:text="帐号:" android:textSize="20sp">
</TextView>
<EditText android:layout_width="70px" android:layout_height="wrap_content"
android:id="@+id/etaccount" android:layout_marginRight="20dip"
android:maxLength="20"></EditText>
</TableRow>
<TableRow android:layout_marginTop="10dip">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tvpw"
android:layout_marginLeft="20dip" android:gravity="center_vertical"
android:text="密码:" android:textSize="20sp">
</TextView>
<EditText android:layout_width="70px" android:layout_height="wrap_content"
android:layout_marginRight="20dip" android:id="@+id/etpw"
android:inputType="textPassword"></EditText>
</TableRow>
</TableLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_marginTop="5dip" android:layout_marginRight="20dip">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tvclear"
android:text="清除Cookies" android:textColor="#aa0000" android:textSize="12px"></TextView>
</LinearLayout>
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="20dip">
<TableRow android:gravity="center" android:layout_width="fill_parent">
<Button android:layout_width="100px" android:layout_height="wrap_content"
android:id="@+id/btnlogin" android:layout_gravity="center"
android:text="登录"></Button>
<Button android:layout_width="100px" android:layout_height="wrap_content"
android:id="@+id/btnexit" android:layout_gravity="center"
android:text="退出"></Button>
</TableRow>
</TableLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_marginTop="25dip">
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/cbrp"
android:text="记住密码" android:textSize="12px"></CheckBox>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/cbal"
android:text="自动登录" android:textSize="12px"></CheckBox>
</LinearLayout>
</LinearLayout>
java代码
package com.wjq;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.wjq.beans.User;
import com.wjq.func.UserMgr;
public class Login extends Activity {
private EditText etAccount;
private EditText etPW;
private Button btnLogin;
private Button btnExit;
private CheckBox cbrp;
private CheckBox cbal;
private UserMgr userMgr;
private User user;
private SharedPreferences sp;
private TextView tvClear;
/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
etAccount = (EditText) findViewById(R.id.etaccount);
etPW = (EditText) findViewById(R.id.etpw);
cbrp = (CheckBox) findViewById(R.id.cbrp);
cbal = (CheckBox) findViewById(R.id.cbal);
btnLogin = (Button) findViewById(R.id.btnlogin);
btnExit = (Button) findViewById(R.id.btnexit);
tvClear=(TextView)findViewById(R.id.tvclear);
InitConfig();
cbrp
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
sp = getSharedPreferences("UserInfo", 0);
sp.edit().putBoolean("cbrp", isChecked).commit();
}
});
cbal
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
sp = getSharedPreferences("UserInfo", 0);
sp.edit().putBoolean("cbal", isChecked).commit();
}
});
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
user = new User(etAccount.getText().toString(), etPW.getText()
.toString());
Log.i("tag", "Account:" + etAccount.getText().toString());
Log.i("tag", "Password:" + etPW.getText().toString());
userMgr = new UserMgr();
Boolean flag = userMgr.CheckUser(user, Login.this);
if (!flag) {
Toast.makeText(Login.this, "用户验证错误!", 1000).show();
}
else {
if (cbrp.isChecked()) {
sp = getSharedPreferences("UserInfo",
Context.MODE_WORLD_WRITEABLE
| Context.MODE_WORLD_READABLE);
sp.edit().putString("account",
etAccount.getText().toString()).commit();
sp.edit().putString("password",
etPW.getText().toString()).commit();
}
}
}
});
btnExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.exit(0);
}
});
tvClear.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {sp=getSharedPreferences("UserInfo", 0);
sp.edit().clear().commit();
}});
}
//初始化配置
private void InitConfig() {
sp = getSharedPreferences("UserInfo", 0);
etAccount.setText(sp.getString("account", null));
etPW.setText(sp.getString("password", null));
cbal.setChecked(sp.getBoolean("cbal", false));
cbrp.setChecked(sp.getBoolean("cbrp", false));
}
}
说明 :
1.写内容
sp = getSharedPreferences("UserInfo", 0);
sp.edit().putBoolean("cbal", isChecked).commit();
UserInfo是指xml文件的文件名,如果此文件已存在则直接向其中写内容“isChecked”的值,首先通过SharedPreferences的edit()方法创建editor,然后调用commit()方法提修改
2.读内容
sp = getSharedPreferences("UserInfo", 0);
etAccount.setText(sp.getString("account", null));
etPW.setText(sp.getString("password", null));
cbal.setChecked(sp.getBoolean("cbal", false));
cbrp.setChecked(sp.getBoolean("cbrp", false));
- RememberNameAndPassword.rar (69.9 KB)
- 下载次数: 30
发表评论
-
ASimpleCache 一个为android制定的 轻量级的 开源缓存框架。
2014-04-10 11:04 1660ASimpleCache 是一个为android制定的 轻量 ... -
android 中 EditText加入图标 更改边框颜色 设置透明 代码
2013-07-19 10:20 3965main.xml [java] view pl ... -
TortoiseGit安装教程
2013-07-17 11:40 1988TortoiseGit 是Windows下的可视化Git界面 ... -
使用WakeLock使Android应用程序保持后台唤醒
2013-07-17 11:02 2938在使用一些产品列如 ... -
Android AlarmManager实现不间断轮询服务
2013-07-17 10:48 2923在消息的获取上是选择轮询还 是推送得根据实际的业务需要来技 ... -
二维码、条形码扫描——使用Google ZXing
2013-07-17 10:20 3599我在项目中用到了二维码扫描的技术,用的是Google提供的Z ... -
android 基于基站,apn,gps,wifi,network 根据不同手机sim卡获取经纬度
2013-05-20 15:08 2243一:新建MyLocation类,本类主要管理使用各种获取经 ... -
android 基于百度地图api开发定位以及获取详细地址
2013-05-20 15:02 2849一:百度地图开发必须要到百度开发平台android开发api ... -
android google地图定位开发,且可以自由移动位置重新获取定位,地址信息
2013-05-20 15:00 2662一:申请key: 1.首先找到debug keyst ... -
Android UI库及组件推荐
2013-03-28 10:40 1788一、UI组件库 1. GreenDroid ... -
Android 启动模拟器是出现“Failed to allocate memory: 8”错误提示的原因及解决办法
2012-11-13 10:00 1737关键词:Android、模拟器、无法启动 某天,Andr ... -
关于Eclpse升级到ADT-20,xml布局文件不能正常显示问题
2012-10-29 17:39 16551.首先要把自己的eclipse中的adt升级到最新的版本(目 ... -
Android开发错误汇总
2012-10-29 17:38 3566大家都在为项目开发成功而喜悦,但可不知成功的路上是会经常出错的 ... -
The method of type must override a superclass method解决方式
2012-09-21 10:18 2209标签:override ActionSupport ... -
[程序猿感悟] Android平台开发中的重构三步走
2012-09-13 16:26 1913我们都知道,技术是优 ... -
Android Layout XML属性
2012-08-16 11:29 1140Layout对于迅速的搭建界面和提高界面在不同分辨率的屏幕 ... -
Eclipse快捷键大全
2012-08-02 09:50 1050Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ct ... -
Android中asset文件夹和raw文件夹区别
2012-07-26 16:58 1923*res/raw和assets的相同点 ... -
分享45个android实例源码,很好很强大.大家赶快来收藏吧!!
2012-07-26 15:55 1080http://www.eoeandroid.com/threa ... -
Intent调用大全,史上最全的
2012-07-26 12:11 1038//调用浏览器 Uri uri = Uri.parse(&q ...
相关推荐
SharedPreferences是Android提供的一个轻量级存储类,经常用于保存软件设置参数。存放的格式为xml,文件存放在 /data/data/<package name>/shared_prefs下。
用户登录后,记住用户名和密码,第二次不用手动输入直接登录,这是很常用的更能,希望能和大家分享。
最近Android项目需要一个自动登录功能,完成之后,特总结一下,此功能依靠SharedPreferences进行实现。 SharedPreferences简介 ...SharedPreferences使用实例:记住用户名密码自动登录 大致了解了SharedPre
使用SharedPreferences 完成记住用户名和密码的基本功能, 而且checkbox的图片本人很喜欢,还有登录界面的shape也是本人精心设计的,很容易和新开发的项目风格保持一致!
主要介绍了Android中使用SharedPreferences完成记住账号密码的功能,需要的朋友可以参考下
使用SharedPreferences保存用户名和密码的方法,并且存在一个CheckBox复选框可以用以保存密码。记录小功能(登录界面记住密码)模块,具体内容可以观看2023-5-7的两篇文章。如果开发者有一点开发基础,可以直接通过...
(1) 掌握Android的SharedPreferences的使用 (2) 掌握在Android中使用SQLite的方法 二、实验内容及步骤 任务:根据下述要求实现对应程序 1、 根据所给界面1完成登入功能,用户名为admin,密码为123456,如果用户名...
实现记住用户名和密码功能的小程序,运用 SharedPreferences实现存储,希望大家多多支持
这段源码直接使用SharedPreferences类。就可以达到保存用户名和密码的功能,代码很短,很容易读懂。移植性很好。
Android登录记住密码功能,其中用户名和密码用SharedPreferences方式保存。 密码用AES算法加密保存。
使用Android Studio开发平台,利用SharedPreferences,在登陆时,若用户勾选记住我的复选框,则在本地生成一个文档记录用户名和密码,下次登陆时免去输入用户名和密码直接登陆。
特点:设计数据库,用户可注册账号,同时保存至数据库... 功能: ... 2记住用户名和密码(SharedPreferences); 3注册账号(SQLite数据库中创建和添加); 4登录系统(Intent); 5查询用户信息(SQLite数据库中查询)。
这个程序是一个Android的登陆界面的设计,它实现了如下功能:记住密码,用户输入时有用户名提示选择。还有管理员界面,添加新用户,修改密码。这里使用的是sharedpreferences和数据库两种方式。