SharedPreferences的使用案例之QQ登录
来源:互联网 发布:qq显示ip软件 编辑:程序博客网 时间:2024/04/25 18:50
在android手机中,可以通过一个“QQ登录”的案例来演示如何使用SharedPreferences存储数据。
1.创建一个名为“QQ登录”的应用程序,将包名修改为com.example.administrator.case_login,设计用户交互界面,布局文件如下:
login_top.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/logintop_roundbg" android:padding="@dimen/activity_horizontal_margin"> <EditText android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/icon_user" android:drawablePadding="10dp" android:ems="10" android:hint="@string/etName"> <requestFocus /> </EditText> <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/etName" android:drawableLeft="@drawable/icon_pass" android:drawablePadding="10dp" android:ems="10" android:hint="@string/etPass" 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/cbIsRemeberPass" android:textSize="20sp" android:layout_weight="1" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btn_select" android:text="@string/btnLogin" /> </LinearLayout></RelativeLayout>
activity_main.xml:
<?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:weightSum="1"> <TextView android:text="Welcome you" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:layout_alignHorizontal="true" android:textSize="40sp"/></LinearLayout>
布局文件中TextView用于提示用户名和密码的文字,EditText用于输入,最后在线性布局下方设置了一个按钮Button,用于单击保存用户名和密码。
2.编写界面交互代码
在loginActivity中,实现当用户输入完QQ号码和密码后,选择记住密码,单击“登录”按钮时调用Utils.saveInfo()方法保存QQ密码。
package com.example.administrator.case_login;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends AppCompatActivity{ private EditText etName; private EditText etPassword; private SharedPreferences sharePreferences; private CheckBox cbIsRememberPass; @Override protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.activity_login); initViews(); sharePreferences=getSharedPreferences("rememberpassword", Context.MODE_PRIVATE); boolean isRemember=sharePreferences.getBoolean("rememberpassword",false); if(isRemember){ String name=sharePreferences.getString("name",""); String password=sharePreferences.getString("password",""); etName.setText(name); etPassword.setText(password); cbIsRememberPass.setChecked(true); } }public void initViews(){ etName=(EditText) findViewById(R.id.etName); etPassword=(EditText) findViewById(R.id.etPassword);} 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=sharePreferences.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,"账号或密码有误",Toast.LENGTH_LONG).show(); } }}
Mainactivity。java:
package com.example.administrator.case_login;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}相关知识点:
使用SharedPreferences类存储数据时,首先需要通过conyext.getSharedPreferences(Stringname.int mode)获取SharedPreferences的实例对象。
SharedPreferences.Editor clear()--------------清空SharedPreferences中的所有数据
boolean.commit()---------------编辑结束,调用该方法提交
SharedPreferences.Editor.edit()---------------创建一个Editor对象
SharedPreferences.Editor remove(String key)-------------删除SharedPreferences指定key所对应的数据
阅读全文
0 0
- SharedPreferences的使用案例之QQ登录
- 数据存储——SharedPreferences的使用与登录案例
- Android登录页面案例(SharedPreferences的使用)
- 实验:使用SharedPreferences实现QQ登录功能
- 数据存储---SharedPreferences之登录案例
- 文件存储的实现-login登录案例3-SharedPreferences
- Android--SharedPreferences的存储和获取数据--案例《登录》
- Android学习历程--sharedpreferences(记住密码的登录案例)
- QQ登录案例
- qq登录案例
- SharedPreferences简单使用案例
- 用户登录记住密码----SharedPreferences的使用
- Android开发之使用SharedPreferences实现QQ登陆的选项框记忆功能(源代码分享)
- Android数据存储和访问之SharedPreferences存储方式_用户登录记住密码案例
- 用户登录记住密码(SharedPreferences的使用)
- 简单的第三方登录之QQ
- 关于 使用 QQ登录 的总结
- Android QQ第三方登录的使用
- sql将查询出的数值变为中文
- 职场必杀技之职场英语
- 火狐使用Firebug查看Post登录的源代码
- 第一个EJB项目(eclipse+Jboss)
- Husky中文文档-C++ Husky 基础
- SharedPreferences的使用案例之QQ登录
- Android开发——关于onCreate的解读
- Husky中文文档-C++ Husky Combiner 指南
- CentOS AliSQL的安装 及与MySQL的差别初探
- jdbc相关知识点
- 读书笔记--MySQL 表对象缓存
- 数字图像处理——用Java将彩色图像转换为灰度图像
- Husky中文文档-C++ Husky Broadcast 和 Request 指南
- CSS开关样式 实现加入推荐和取消推荐