功能:输入用户名和密码,正确,显示登录成功,为空的话,提示用户名和密码不能为空,还有记住密码功能。

MainActivity.java

package com.aimee.android.play.qqlogin;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader; import static android.text.TextUtils.isEmpty; public class MainActivity extends AppCompatActivity { private EditText mEtnumber;
private EditText mEtPasswd;
private CheckBox mCbRemember; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mEtnumber = findViewById(R.id.et_qq);
mEtPasswd = findViewById(R.id.et_password);
mCbRemember = findViewById(R.id.cb_remember); //1.找到界面上的控件
//2.点击按钮,将用户输入的用户名和密码保存到文件当中
//3.如果说下次启动app,那么就从文件当中读取出来显示到编辑框 restoreInfo();
} /**
* 根据原来保存的文件信息,把QQ号码和密码信息显示到界面
* */
private void restoreInfo() {
File file = new File(this.getFilesDir(), "info.txt");
//如果文件存在并且有内容就读取出来
if (file.exists() && file.length() > 0){
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String info = br.readLine();
String qq = info.split("##")[0];
String pwd = info.split("##")[1];
mEtnumber.setText(qq);
mEtPasswd.setText(pwd);
} catch (Exception e) {
e.printStackTrace();
}
}
} /**
* 登录按钮的点击事件
*
* @param view
* */
public void login(View view) {
String qq = mEtnumber.getText().toString().trim();
String password = mEtPasswd.getText().toString().trim(); if (isEmpty(qq) || isEmpty(password)){
Toast.makeText(this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
return;
}else {
//判断是否需要记录用户名和密码
if (mCbRemember.isChecked()){
// Toast.makeText(this,this.getExternalFilesDir(null).getAbsolutePath(),Toast.LENGTH_SHORT).show();
//被选中的状态,需要记录用户名和密码
File file = new File(this.getFilesDir(),"info.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
String info = qq + "##" + password;
fos.write(info.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
} //模拟登录,当前输入的用户名和密码联网操作对比云端数据库是否匹配
if ("10000".equals(qq) && "123456".equals(password)){
Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show();
}
}
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="108dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/qq" /> <EditText
android:id="@+id/et_qq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="请输入QQ号码"
android:gravity="center"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" /> <EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="请输入密码"
android:gravity="center"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_qq" /> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="88dp"
android:layout_marginEnd="8dp"
android:onClick="login"
android:text="登录"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_password" /> <CheckBox
android:id="@+id/cb_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="记住密码"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_password" />
</android.support.constraint.ConstraintLayout>

提醒getFilesDir()路径下面的文件可能没有执行权限,可以试试采用adb shell的方式,输入su请求root权限,手机会弹出是否允许,记住不要看错了,默认可是拒绝,本人就是手快点了个永久拒绝。

第四十四篇--做一个简单的QQ登录界面的更多相关文章

  1. 程序猿修仙之路--数据结构之你是否真的懂数组? c#socket TCP同步网络通信 用lambda表达式树替代反射 ASP.NET MVC如何做一个简单的非法登录拦截

    程序猿修仙之路--数据结构之你是否真的懂数组?   数据结构 但凡IT江湖侠士,算法与数据结构为必修之课.早有前辈已经明确指出:程序=算法+数据结构  .要想在之后的江湖历练中通关,数据结构必不可少. ...

  2. 第四章 .net core做一个简单的登录

    项目目标部署环境:CentOS 7+ 项目技术点:.netcore2.0 + Autofac +webAPI + NHibernate5.1 + mysql5.6 + nginx 开源地址:https ...

  3. ASP.NET MVC如何做一个简单的非法登录拦截

    摘要:做网站的时候,经常碰到这种问题,一个没登录的用户,却可以通过localhost:23244/Main/Index的方式进入到网站的内部,查看网站的信息.我们知道,这是极不安全的,那么如何对这样的 ...

  4. 第二十八篇 -- 写一个简陋的WIFI服务器界面

    效果图: Dlg.cpp // WIFIWMITestDlg.cpp : implementation file // #include "stdafx.h" #include & ...

  5. 使用Multiplayer Networking做一个简单的多人游戏例子-2/3(Unity3D开发之二十六)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51007512 ...

  6. ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁

    作者:Grey 原文地址: ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁 前置知识 完成ZooKeeper集群搭建以及熟悉ZooKeeperAPI基本使用 需求 当多个进 ...

  7. 使用Multiplayer Networking做一个简单的多人游戏例子-1/3(Unity3D开发之二十五)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 ...

  8. tkinter做一个简单的登陆页面(十六)

    做一个简单的登陆页面 import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry("900x3 ...

  9. NeHe OpenGL教程 第四十四课:3D光晕

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

随机推荐

  1. 如何利用U盘重装系统

    第一步,下载系统镜像 推荐在msdn上面下载,因为大多数都是 Microsoft 纯净原版镜像,如果要安装的是纯净版系统请先看第六步,然后才看第二步 第二步,下载U盘PE工具 推荐使用大白菜或者老毛桃 ...

  2. Git配置用户信息和SSH

    安装完成后,在开始菜单里找到“Git”->“Git Bash”,蹦出一个类似命令行窗口的东西,就说明Git安装成功! 1.配置用户信息 在命令行输入: $ git config --global ...

  3. PyQtdeploy-V2.4 User Guide 中文 (二)

    PyQtdeploy 用户指南 目录 介绍 与V1.0+的差异 作者 证书 安装 部署过程概览 PyQt的演示 构建演示 Android IOS Linux MacOS Windos 构建系统根目录 ...

  4. web开发布局---传统布局篇

    1.传统布局 盒状模型结合 display 属性.float 浮动以及 position 定位属性设计的各式传统布局形式. 2.说再多不如动手实践,下面举三个例子 html 部分代码: <sec ...

  5. 基于Xamarin Android实现的简单的浏览器

    最近做了一个Android浏览器,当然功能比较简单,主要实现了自己想要的一些功能……现在有好多浏览器为什么还要自己写?当你使用的时候总有那么一些地方不如意,于是就想自己写一个. 开发环境:Xamari ...

  6. css选择器概述

    css选择器种类 id选择器 类选择器.属性选择器.伪类选择器 元素选择器.伪元素选择器 通配符选择器.子类选择器.后代选择器.相邻兄弟选择器.选择器分组 一.id选择器 <p id=" ...

  7. 一起学Android之Intent

    本文简述在Android开发中Intent的常见应用,仅供学习分享使用. 什么是Intent? Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Inten ...

  8. 新浪短连接API免登陆免认证实例

    string source = "source=2849184197"; // APP Key,这个可以根据自己需要去网上搜索 string url_long = $"u ...

  9. 测者的测试技术手册:Junit单元测试遇见的一个枚举类型的坑(枚举类型详解)

    Enum的简介 枚举类型很早就在计算机语言中存在了,主要被用来将一组相似的值包含进一种类型中,这种类型的名称被定义成独一无二的类型描述符,这就是枚举类型. 在java语言中,枚举类型是一个完整功能的类 ...

  10. 阿里云MySQL远程连接不上问题

    解决阿里云MySQL远程连接不上的问题:step1:1.修改user表:MySQL>update user set host = '%' where user = 'root'; 2.授权主机访 ...