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

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. IDEA XML注释与取消注释快捷键

    IntelliJ IDEA和eclipse中编辑Java文件时,注释和取消注释的快捷键都是: "CTRL + / " 编辑xml文件时, 注释:CTRL + SHIFT + / 取 ...

  2. 手把手教新手小白在window把自己的项目上传到github

    作为一个开发者,写博客,上传项目到github好像是不可不会的技能,很多有经验的老司机都会这么建议你.本宝宝第一次要把项目传到github的时候,确实有点蒙蔽,什么鬼,传个东西有必要这么难吗? git ...

  3. websocket简单实现在线聊天

    WebSocket简介与消息推送 B/S架构的系统多使用HTTP协议,HTTP协议的特点: 1 无状态协议2 用于通过 Internet 发送请求消息和响应消息3 使用端口接收和发送消息,默认为80端 ...

  4. MongoDB- 简单操作命令

    MongoDB是基于集合操作的数据库 1.进入与退出 mongo / exit 2.库操作 显示所有库: show dbs; 查看当前所在库: db; 切换&使用某个库: use db_nam ...

  5. 【shell基础】if分支语句

    1.if判断式if [ 条件判断一 ] && (||) [ 条件判断二 ]; thenelif [ 条件判断三 ] && (||) [ 条件判断四 ]; thenels ...

  6. apache ranger源码编译

    官方文档 http://ranger.apache.org/quick_start_guide.html Quick Start Guide Build Process 1. Check out th ...

  7. 关键字-this

    1.当成员变量和局部变量重名时,在方法中使用this时,this指向的是该方法所在类的成员变量. package gc.test.java.cs1; public class User{ public ...

  8. MySQL replace into (insert into 的增强版)

    在使用SQL语句进行数据表插入insert操作时,如果表中定义了主键,插入具有相同主键的记录会报错:  Error Code: 1062. Duplicate entry 'XXXXX' for ke ...

  9. spring【一】 学习

    Spring 源码学习 通过注解的形式注入IOC 简单的创建一个maven的项目的 下载指定的spring的核心jar包(https://mvnrepository.com/artifact/org. ...

  10. 初识gauge自动化测试框架

    segmentfault阅读 官方网站:https://docs.gauge.org/latest/index.html 介绍: Gauge是一个轻量级的跨平台测试自动化工具,可以使用不同的语言中编写 ...