完成了界面的一些基础判断以及记住密码功能并没有实现真正的的登录
在main_activity中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"
android:hint="请输入密码"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="@+id/ck_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<Button
android:onClick="login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
/>
<!-- android:layout_toRightOf="@id/ck_cb"-->
</RelativeLayout>
</LinearLayout>
然后是定义了一个工具类用来存储用户信息和读取存储的用户
package com.example.administrator.logindemon;
import android.content.Context;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2016-06-20.
*/
public class UserInfoutils {
public static boolean saveInfo(Context context,String username,String pwd){
try{
//获取文件保存的目录
//String path = context.getFilesDir().getPath();
String result = username + "##" + pwd;
//1.创建file类指定我们要数据存储的位置
// File file = new File("/data/data/com.example.administrator.logindemon/info.txt");
//File file = new File(path,"info.txt");
//2.创 建一个输出流
// FileOutputStream fos = new FileOutputStream(file);
//3.使用上下文获取FileOutputStream
FileOutputStream fos = context.openFileOutput("info1.text",0);
fos.write(result.getBytes());
fos.close();
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
//读取用户信息
public static Map<String,String> readInfo(Context context) {
try {
//定义map
Map<String, String> maps = new HashMap<String, String>();
FileInputStream fis = context.openFileInput("info1.txt");
// String path = context.getFilesDir().getPath();
// File file = new File("/data/data/com.example.administrator.logindemon/info.txt");
//File file = new File(path,"info.txt");
// FileInputStream fis = new FileInputStream(file);
BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));
String content = bufr.readLine();//读取数据
//切割字符串封装到map集合中
String[] splits = content.split("##");
String name = splits[0];
String pwd = splits[1];
//把name和pwd放入map中
maps.put("name",name);
maps.put("pwd",pwd);
fis.close();;
return maps;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
最后是MainActivity.java文件中代码如下
package com.example.administrator.logindemon;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private EditText username;
private EditText password;
private CheckBox cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
username = (EditText) findViewById(R.id.et_username);
password = (EditText) findViewById(R.id.et_password);
cb = (CheckBox) findViewById(R.id.ck_cb);
//1.1读取/data/data/com.example.administrator.logindemon/info.txt文件
// Map<String,String> maps = UserInfoutils.readInfo();
Map<String,String> maps = UserInfoutils.readInfo(MainActivity.this);
if(maps!=null){
//把name和pwd取出来
String name = maps.get("name");
String pwd = maps.get("pwd");
//name把pwd在EditText控件上显示出来
username.setText(name);
password.setText(pwd);
}
}
//写按钮点击事件
public void login(View v){
//2.1:获取用户名,密码
String name = username.getText().toString().trim();
String pwd = password.getText().toString().trim();
//2.2:判断是否为用户名密码是否为为空
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
Toast.makeText(MainActivity.this,"用户名和密码不能为空",Toast.LENGTH_LONG).show();
}else{
//2.3进行登录逻辑的撰写
System.out.println("链接服务器,以后再说");
if(cb.isChecked()){
// System.out.println("我会记住账号密码");
//使用上下文
boolean result = UserInfoutils.saveInfo(MainActivity.this,name,pwd);
// boolean result = UserInfoutils.saveInfo(name,pwd);
if(result){
Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this,"保存失败",Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(MainActivity.this, "请勾选记住密码", Toast.LENGTH_LONG).show();
}
}
}
}