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

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. Java 平台无关性的基石

    Java 在刚刚诞生之初就提出过一个非常著名的口号:"一次编写,到处运行", 这句话充分表达了软件开发人员对冲破平台界限的渴望 在竞争激烈的 IT 领域,各种不同的硬件体系结构和不 ...

  2. 折腾Java设计模式之观察者模式

    观察者模式 Define a one-to-many dependency between objects where a state change in one object results in ...

  3. 前端零基础 --css转换--skew斜切变形 transfor 3d

    前端零基础 --css转换--skew斜切变形 transfor 3d==============重要不紧急! 重要紧急 重要不紧急 不重要紧急 不重要不紧急

  4. 基于Html5 Plus + Vue + Mui 移动App开发(三)-文件操作(读取、保存、更新数据)

      随着手机的发展,现在越来越多的人选择在手机上看书.无论是专业书籍.文学.英语还是网络小说,在手机上看新闻成了人们处理零碎时间的办法.在智能手机里安装一个资讯APP,可以随时.随地查看自己想看的资讯 ...

  5. 服务器四:多进程epoll

    #include <fcntl.h> #include <sys/socket.h> #include <netinet/in.h> #include <ar ...

  6. openlayers二:添加矢量图形文字

    openlayers可方便的在地图上添加圆.多边形.文字等矢量内容,修改这些矢量内容的样式也很简单. 首先需要添加一个向量图层: initVectorLayer: function () { this ...

  7. UI第三方

    自定义下拉刷新控件 - RefreshableView(支持所有控件的下拉刷新)https://blog.csdn.net/cjh_android/article/details/52462367 亲 ...

  8. gitbook 入门教程之主题插件

    主题插件 目前 gitbook 提供三类文档: Book 文档,API 文档和 FAQ 文档. 其中,默认的也是最常使用的就是 Book 文档,如果想要了解其他两种文档模式,需要引入相应的主题插件. ...

  9. iOS:我的学习路径

    1.复习C语言(半个月) <C Primer Plus>1-6章 2.学习Objective-C基础语法(一周) 黑马程序员视频 3.直接用Xcode开始APP的实战(半个月) 黑马程序员 ...

  10. SQL ----post漏洞测试注入

    使用工具sqlmap 输入账号密码进行bp截断,获取文本保存在sqlmap下面2.txt 爆数据库 爆表爆表 爆数据 最后把数据密码md5解析