Android通过

Android通过,第1张

概述SharedPreferences是Android中存储简单数据的一个工具类。可以想象它是一个小小的Cookie,它通过用键值对的方式把简单数据类型(boolean、int、float、long和String)存储在应用程序的私有目录下(data/data/包名/sha

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,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",isChecked).commit();  UserInfo是指xml文件的文件名,如果此文件已存在则直接向其中写内容“isChecked”的值,首先通过SharedPreferences的edit()方法创建editor,然后调用commit()方法提修改

2.读内容

sp = getSharedPreferences("UserInfo",false));

以上就是本文的全部内容,希望对大家的学习有所帮助。

总结

以上是内存溢出为你收集整理的Android通过全部内容,希望文章能够帮你解决Android通过所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1149577.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存