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

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. Gulp 前端优化

    使用方法: 下载 node.js , https://nodejs.org/en/,并安装 msi 一下命令都属于 dos 命令 node -v,npm -v,检验是否下载成功(出现版本号) 将 np ...

  2. python 练习 后台返回当前时间

    新建一个 current_time.html 文件, !cur_time! 用来替换 <!DOCTYPE html> <html lang="en"> &l ...

  3. Android 最简单的测试UI卡顿

    就两个类: public class BlockDetectByPrinter { private static final String START = ">>>> ...

  4. Android 设计模式之MVC模式

    说到Android设计模式的MVC模式,估计很多人都是比较熟悉了,这里深入了解一下MVC到底是怎么回事,以ListView为例子讲解. 一.深入理解MVC概念 MVC即Model-View-Contr ...

  5. Ubuntu16.04下搭建mysql + uwsgi + nginx环境启动flask 项目

    1.安装mysql Sudo apt-get install mysql 配置mysql的数据存储路径,默认在 /var/lib/mysql sudo cp -R /var/lib/mysql/* / ...

  6. Objective-C Block与函数指针比较

    相似点 1.函数指针和Block都可以实现回调的操作,声明上也很相似,实现上都可以看成是一个代码片段. 2.函数指针类型和Block类型都可以作为变量和函数参数的类型.(typedef定义别名之后,这 ...

  7. 桌面远程连接阿里云服务器(windows)后丧失了双向文件复制粘贴功能的解决方案(第一条博客!)

    近日应公司要求,需在windows服务器上架设一个交易中介软件. 过程之一:将软件压缩文件传到服务器上. 问题:在“运行”对话框通过输入'mstsc' 创建远程连接以后,出现本地桌面与服务器之间无法物 ...

  8. centos7 最小安装初始化

    配置阿里yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup \&&cu ...

  9. Ranger-Kafka插件安装

    Ranger-Kafka插件安装, 使用Ranger0.7.0版本,集成Kafka插件到Kafka集群, Kafka Plugin需要安装到所有的Kafka的集群节点上面. 1.登陆Kafka的安装用 ...

  10. js 秒数格式化

    function formatSeconds(value) { var theTime = parseInt(value);// 秒 var theTime1 = 0;// 分 var theTime ...