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

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. (五) Keras Adam优化器以及CNN应用于手写识别

    视频学习来源 https://www.bilibili.com/video/av40787141?from=search&seid=17003307842787199553 笔记 Adam,常 ...

  2. 基于python的种子搜索网站-开发过程

    本讲会对种子搜索网站的开发过程进行详细的讲解. 源码地址:https://github.com/geeeeeeeek/bt 项目开发过程 项目简介 该项目是基于python的web类库django开发 ...

  3. python类的继承、封装和多态

    摘自https://www.cnblogs.com/evablogs/p/6724477.html 继承 1 2 3 4 5 6 7 8 class Person(object):     def _ ...

  4. THINKphp中复杂的查询

    字符串拼接查询 案例一:拼接字符串(多条件查询) $where = ''; //定义字符串,用于拼接满足条件的数据字段 $value = []; // 定义空数组,用于接收值 if(!empty($n ...

  5. windows10滑轮bug

    今天我突然发现我一点也忍受不了在UWP应用.wi10窗口.设置等界面无法使用鼠标滑轮了.这个bug已经困扰了我差不多一年了,从买了这台笔记本就开始了.而且这个问题在中间的某一次升级系统后,也修复了,但 ...

  6. Java基础系列--02_运算符和程序的语句

    运算符: (1)算术运算符: +,-,*,/,%,++,--(加.减.乘.除.取余.自增,自减) ++和--的注意事项: a:他们的作用是自增或者自减 b:使用 1.单独使用 放在操作数据的前面和后面 ...

  7. 在html代码中js的script标签建议放在那里?

    今天编写了一个简单的js代码,F12有错误,然后发现是<script>放的位置有问题.之前在我的印象当中,说的是这个标签放在哪里都可以,然而...并不是这样的,例如我现在练习的这个代码,写 ...

  8. Java 8 Stream介绍及使用2

    (原) stream中另一些比较常用的方法. 1. public static<T> Stream<T> generate(Supplier<T> s) 通过gen ...

  9. 【转】HTTP请求中的form data和request payload的区别

    jQuery的ajax方法和post方法分别发送请求,在后台Servlet进行处理时结果是不一样的,比如用$.ajax方法发送请求时(data参数是一个JSON.stringify()处理后的字符串, ...

  10. LVS调度算法

    LVS-四层调度 1.轮询算法:Round Robin - RR 后端RS性能一致,请求开销差别小 2.加权轮询:Weighted Round Robin - WRR 后端RS性能有差异,请求开销差异 ...