Andriod Studio实现保存QQ密码功能(案例代码详解)

所属分类: 软件编程 / Android 阅读数: 56
收藏 0 赞 0 分享

对于QQ登录时保存账号和密码的功能,不仅文件存储能够实现,SharePreferences同样也可以实现,而且SharedPreferences存取数据更加简单方便。因此可以用该方法实现保存Q密码的案例,具体步骤如下:

创建布局类

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.kh11.MainActivity">

  <ImageView
    android:id="@+id/iv"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:background="@drawable/touxiang"/>
  <LinearLayout
    android:id="@+id/ll_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/iv"
    android:layout_centerVertical="true"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="15dp"
    android:background="#ffffff">
    <TextView
      android:id="@+id/tv_number"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="10dp"
      android:text="账号:"
      android:textColor="#000"
      android:textSize="20sp"/>
    <EditText
      android:id="@+id/et_number"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="5dp"
      android:background="@null"
      android:padding="10dp"/>
  </LinearLayout>
  <LinearLayout
    android:id="@+id/ll_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ll_number"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="#ffffff">
    <TextView
      android:id="@+id/tv_password"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="10dp"
      android:text="密码:"
      android:textColor="#000"
      android:textSize="20sp"/>
    <EditText
      android:id="@+id/et_password"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="5dp"
      android:background="@null"
      android:inputType="textPassword"
      android:padding="10dp"/>
  </LinearLayout>
  <Button
    android:id="@+id/btn_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ll_password"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="50dp"
    android:background="#3C8DC4"
    android:text="登录"
    android:textColor="#ffffff"
    android:textSize="20sp"/>
</RelativeLayout>

创建工具类

package cn.itcast.saveqq;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

public class SPSaveQQ {
  public static boolean saveUserInfo(Context context,String number,String password){
    SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE);
    SharedPreferences.Editor edit = sp.edit();
    edit.putString("userName",number);
    edit.putString("pwd", password);
    edit.commit();
    return true;
  }
  public static Map<String ,String> getUserInfo(Context context){
    SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE);
    String number = sp.getString("userName", null);
    String password = sp.getString("pwd", null);
    Map<String ,String > userMap = new HashMap<String,String>();
    userMap.put("number",number);
    userMap.put("password",password);
    return userMap;
  }
}

编写界面交互代码

package com.example.kh11;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Map;
import cn.itcast.saveqq.SPSaveQQ;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  private EditText etNumber;
  private EditText etPassword;
  private Button btnLogin;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //初始化界面
    initView();
    Map<String ,String > userInfo = SPSaveQQ.getUserInfo(this);
    if(userInfo != null){
      etNumber.setText(userInfo.get("number"));
      etPassword.setText(userInfo.get("password"));
    }
  }
  private void initView(){
    etNumber = (EditText) findViewById(R.id.et_number);
    etPassword = (EditText) findViewById(R.id.et_password);
    btnLogin = (Button) findViewById(R.id.btn_login);
    //设置按钮的点击事件
    btnLogin.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
    //当单机登录按钮时,获取QQ账号和密码
    String number = etNumber.getText().toString().trim();
    String password = etPassword.getText().toString();
    //检验账号和密码是否正确
    if(TextUtils.isEmpty(number)){
      Toast.makeText(this,"请输入QQ账号",Toast.LENGTH_SHORT).show();
      return;
    }
    if(TextUtils.isEmpty(password)){
      Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
      return;
    }
    //登陆成功
    Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
    //保存用户信息
    boolean isSaveSuccess = SPSaveQQ.saveUserInfo(this,number,password);
    if(isSaveSuccess){
      Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
    }else{
      Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
    }
  }
}

运行程序

程序运行成功后,在界面输入账号和密码,单击登录按钮,会弹出“登陆成功”和“保存成功”字样,数据信息会保存在SharedPreferences中,可以在data.xml文件中查看保存的数据信息。
运行结果如图:
(这个上传的图片怎么改尺寸,真的太丑了。。。)

在这里插入图片描述

更多精彩内容其他人还在看

Android 自定义球型水波纹带圆弧进度效果(实例代码)

最近小编接到一个这样的需求,需要实现一个圆形水波纹,带进度,两层水波纹需要渐变显示,且外围有一个圆弧进度。今天小编给大家分享实例代码,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Flutter 实现下拉刷新上拉加载的示例代码

这篇文章主要介绍了Flutter 实现下拉刷新上拉加载的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Windows实现Flutter环境搭建及配置这一篇就够了

这篇文章主要介绍了Windows实现Flutter环境搭建及配置这一篇就够了,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android利用碎片fragment实现底部标题栏(Github模板开源)

Fragment可以作为Activity的组成部分,一个Activity可以有多个Fragment,这篇文章主要介绍了Android利用碎片fragment实现底部标题栏(Github模板开源),需要的朋友可以参考下
收藏 0 赞 0 分享

android studio 的下拉菜单Spinner使用详解

这篇文章主要介绍了android studio 的下拉菜单Spinner使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解析Android 8.1平台SystemUI 导航栏加载流程

这篇文章主要介绍了Android 8.1平台SystemUI 导航栏加载流程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信录音功能

这篇文章主要为大家详细介绍了Android仿微信录音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信键盘切换效果

这篇文章主要为大家详细介绍了Android仿微信键盘切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android超清晰6.0权限申请AndPermission

这篇文章主要介绍了Android超清晰6.0权限申请AndPermission,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信录制语音功能

这篇文章主要介绍了Android仿微信录制语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多