版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中使用SharedPreferences实现保存登录数据功能
这篇文章给大家介绍怎么在Android中使用SharedPreferences实现保存登录数据功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1.activity_main.xml页面存放所有的控件,我在每一行都使用了线性布局。activity_main.xml页面:<RelativeLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
tools:context=".SecondActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
/>
<EditText
android:id="@+id/etInputName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tvPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密
码:"
/>
<EditText
android:id="@+id/etInputPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/cbSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="保存用户名"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:orientation="horizontal"
>
<Button
android:id="@+id/btLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录"
/>
<Button
android:id="@+id/btCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"
/>
</LinearLayout>
</RelativeLayout>2.MainActivity.java页面处理登录和保存数据。MainActivity.java页面:package
com.example.sharedpreferences;
import
android.os.Bundle;
import
android.app.Activity;
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.EditText;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity
implements
OnClickListener{
SharedPreferences
pref;
Editor
editor;
private
EditText
etInputName,etInputPass;
private
CheckBox
ckSave;
private
Button
btLogin,btCancel;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
*
获取控件id
*/
getId();
/**
*
保存数据
*/
saveData();
/**
*
绑定点击事件
*/
bindClick();
}
/**
*
获取控件id
*/
private
void
getId()
{
etInputName=(EditText)
findViewById(R.id.etInputName);
etInputPass=(EditText)
findViewById(R.id.etInputPass);
ckSave=(CheckBox)
findViewById(R.id.cbSave);
btLogin=(Button)
findViewById(R.id.btLogin);
btCancel=(Button)
findViewById(R.id.btCancel);
}
/**
*
保存数据
*/
private
void
saveData()
{
//获得SharedPreferences对象
pref=getSharedPreferences("userInfo",MODE_PRIVATE);//将内容存放到名为userInfo的文档内
//获得SharedPreferences.Editor对象
editor=pref.edit();
String
name=pref.getString("userName","");//获取用户名
if(name.equals("")){//如果name为空,代表未选择保存用户名
ckSave.setChecked(false);//不勾选
}else{
ckSave.setChecked(true);
etInputName.setText(name);//将读取到的name值赋值到EditText中
}
}
/**
*
绑定点击事件
*/
private
void
bindClick()
{
btLogin.setOnClickListener(this);
btCancel.setOnClickListener(this);
}
/**
*
按钮点击事件
*/
@Override
public
void
onClick(View
view)
{
switch
(view.getId())
{
case
R.id.btLogin:
String
name=etInputName.getText().toString().trim();//获取输入的名字并去掉空格
String
pass=etInputPass.getText().toString().trim();//获取输入的密码并去掉空格
if("admin".equals(name)&&"123456".equals(pass)){
if(ckSave.isChecked()){//如果选择保存用户名
editor.putString("userName",name);
mit();//提交数据
}else{//如果未选择保存用户名
editor.remove("userName");//删除用户名
mit();//提交数据(每次更改都需要提交)
}
Toast.makeText(SecondActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(SecondActivity.this,"用户名或密码不正确",Toast.LENGTH_SHORT).show();
}
break;
case
R.id.btCancel:
break;
}
}
}3.保存的文件目录可以查看的到,点击右上角进入,找到data->data->当前目录的包名->shared-prefs->新建的文件名4.另外,点击右上角导出可以暂时保存到桌面,然后选择打开方式可以查看里边信息。5.还有一点是,当程序在真机上运行时,fileexplore
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 第1课 多样的数字资源-同步练习
- 2024春夏童装防晒衣白皮书
- 车间级班组级安全培训教材
- 高考生物一轮复习 第一单元 走近细胞和组成细胞的分子 第1讲 走近细胞-人教版高三全册生物试题
- 小升初英语必做语法200题(附答案)
- 水稻秧田病虫害防治技术意见
- 8.3 正确对待外来文化 课件-高中政治统编版必修四哲学与文化
- 2024届河北省高三下学期适应性测试(二模)英语试题
- 新高考(九省联考模式)2024年全国第四届章鱼杯联考高中组数学试题+答案
- 电气控制及S7-1200 PLC应用技术(第二版)课件 第5、6章 S7-1200PLC扩展指令、PLC逻辑控制系统编程实例
- 自助小火锅开店计划书(课堂PPT)
- 国家质量监测应急预案
- 部编本人教版语文八年级下册生字表(精校版)(教师版、学生版) (1)
- 应急预案评审记录表(全)
- 智能停车诱导系统技术和设置规范
- 日语语法大全整理笔记
- 高二3班期中考试家长会.ppt
- 涂料和油漆稀释剂配方
- 职工代表大会操作流程图
- 布朗运动及其应用
- 最全流程框架-美国生产力和质量中心APQC
评论
0/150
提交评论