布局:

线性布局+相对布局

日志打印:

利用LogCat和System.out.println打印观察。

Onclick事件是采用过的第四种:

在配置文件中给Button添加点击时间

涉及知识:

通过上线文context获得文件的路径和缓存路径,保存文件

布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.loginUI.MainActivity"
tools:ignore="MergeRootFrame"
android:orientation="vertical"> <TextView
android:id="@+id/tv_plInputName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plInputName" /> <EditText
android:id="@+id/et_userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText> <TextView
android:id="@+id/tv_plInputPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plInputPassword" /> <EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"/> <RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox
android:checked="true"
android:id="@+id/cb_rmPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rmPassword" /> <Button
android:onClick="login"
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="35dp"
android:text="@string/login" /> </RelativeLayout> </LinearLayout>

MainActivity代码:

package com.example.loginUI;

import java.util.Map;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; import com.example.service.LoginService; public class MainActivity extends ActionBarActivity { //日志记录Tag
private String TAG = "MainActivity"; /** 用户名 */
private EditText etUserName; /** 密码 */
private EditText etPassword; /** 登陆按钮 */
private Button btnLogin; /** 记住密码按钮 */
private CheckBox cbx; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //获得控件
etUserName = (EditText)findViewById(R.id.et_userName);
etPassword = (EditText)findViewById(R.id.et_password);
btnLogin = (Button)findViewById(R.id.btn_login);
cbx = (CheckBox)findViewById(R.id.cb_rmPassword); Map<String, String> result = (new LoginService().getUserNameAndPassword(this));
if (null != result ) {
etUserName.setText(result.get("userName"));
etPassword.setText(result.get("password"));
}
} public void login(View view) {
//日志打印
Log.i(TAG, "开始登陆验证"); String userName = etUserName.getText().toString();
String password = etPassword.getText().toString();
//非空判断给出吐司提示
if (TextUtils.equals(userName.trim(), "") || TextUtils.equals(password.trim(), "")) {
Toast.makeText(this, "用户名/密码不能为空", Toast.LENGTH_SHORT).show();
return ;
}
//是否保存密码
if (cbx.isChecked()) {
//new LoginService().saveUserNameAndPassword(userName, password);
new LoginService().saveUserNameAndPassword(this, userName, password);
}
if("zz".equals(userName) && "11".equals(password)) {
Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "登陆失败", Toast.LENGTH_SHORT).show();
}
Log.i(TAG, "登陆验证完成"); } }

保存数据以及读数据的代码:

package com.example.service;

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.List;
import java.util.Map; import android.content.Context;
import android.util.Log; public class LoginService { private String TAG = "loginService"; /**
* 保存用户名密码, 这样的方式不灵活, 如果我们改了工程的包名的话, 这里就变成了我们的工程往另一工程写数据了, 这是不允许的
* @param userName
* @param password
* @return
*/
public boolean saveUserNameAndPassword(String userName, String password) {
Log.i(TAG, "开始保存用户名密码"); File file = new File("/data/data/com.example.loginUI/info.txt");
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(file);
outputStream.write((userName+"#"+password).getBytes());
outputStream.close();
} catch (Exception e) {
Log.e(TAG, "保存用户名密码出现异常");
return false;
}
return true;
} /**
* 保存用户名密码, 通过上下文动态的改变文件路径
* @param context
* @param userName
* @param password
* @return
*/
public boolean saveUserNameAndPassword(Context context, String userName, String password) {
Log.i(TAG, "开始保存用户名密码"); File file = new File(context.getFilesDir(), "info.txt"); // == File file = new File("/data/data/com.example.loginUI/files/info.txt"); //File file = new File(context.getCacheDir(), "info.txt"); // /data/data/com.example.loginUI/cache/info.txt 放进缓存,不要放太大的东西
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(file);
outputStream.write((userName+"#"+password).getBytes());
outputStream.close();
} catch (Exception e) {
Log.e(TAG, "保存用户名密码出现异常");
return false;
}
return true;
} public Map<String, String> getUserNameAndPassword(Context context) {
Map<String, String> result = new HashMap<String, String>();
File file = new File(context.getFilesDir(), "info.txt");
FileInputStream fis;
try {
fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String[] lists = br.readLine().split("#");
Log.i(TAG, "要保存的用户名="+lists[0]+": 密码="+lists[1]);
result.put("userName", lists[0]);
result.put("password", lists[1]);
} catch (Exception e) {
e.printStackTrace();
}
return result;
} }

Android简单登陆页面的更多相关文章

  1. Android 简单登陆 涉及 Button CheckBox TextView EditText简单应用

    GitHub地址:https://github.com/1165863642/LoginDemo 直接贴代码<?xml version="1.0" encoding=&quo ...

  2. Android 使用 intent 实现简单登陆页面

    前言 第一个 Android 程序,应该有些纪念的意义吧~ 主页面布局 给 Button 添加响应函数:android:onClick="login" public void lo ...

  3. android简单登陆和注册功能实现+SQLite数据库学习

    最近初学android,做了实验室老师给的基本任务,就是简单的登陆和注册,并能通过SQLite实现登陆,SQlLite是嵌入在安卓设备中的 好了下面是主要代码: 数据库的建立: 这里我只是建立了一个用 ...

  4. Android笔记-4-实现登陆页面并跳转和简单的注册页面

    实现登陆页面并跳转和简单的注册页面   首先我们来看看布局的xml代码 login.xml <span style="font-family:Arial;font-size:18px; ...

  5. 小KING教你做android项目(二)---实现登陆页面并跳转和简单的注册页面

    原文:http://blog.csdn.net/jkingcl/article/details/10989773       今天我们主要来介绍登陆页面的实现,主要讲解的就是涉及到的布局,以及简单的跳 ...

  6. .Net程序猿玩转Android开发---(3)登陆页面布局

    这一节我们来看看登陆页面如何布局.对于刚接触到Android开发的童鞋来说.Android的布局感觉比較棘手.须要结合各种属性进行设置,接下来我们由点入面来 了解安卓中页面如何布局,登陆页面非常eas ...

  7. tkinter做一个简单的登陆页面

    做一个简单的登陆页面 import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry("900x3 ...

  8. tkinter做一个简单的登陆页面(十六)

    做一个简单的登陆页面 import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry("900x3 ...

  9. python编写简单的html登陆页面(4)

    python编写简单的html登陆页面(4)   1  在python编写简单的html登陆页面(2)的基础上在延伸一下: 可以将动态态分配数据,建立表格,存放学生信息 2 实现的效果如下: 3  动 ...

随机推荐

  1. java多线程中的生产者与消费者之等待唤醒机制@Version2.0

    二.生产者消费者模式的学生类成员变量生产与消费demo, @Version2.0 在学生类中添加同步方法:synchronized get()消费者,synchronized set()生产者 最终版 ...

  2. Java中如何在另一个类里面使用运行类中的对象,举例说明了一下。

    package 计时器; import java.util.Timer; import java.util.TimerTask; /* * 主要是想在另一个类里面,使用该类的对象,如何使用呢?如何传递 ...

  3. HDU 2236:无题II(二分搜索+二分匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2236 题意:中文题意. 思路:先找出最大和最小值,然后二分差值,对于每一个差值从下界开始枚举判断能不能二分匹配. ...

  4. mime类型表

    types {    text/html                             html htm shtml;    text/css                         ...

  5. 【转】在Eclipse中建立第一个Servlet程序

    转载地址:http://kin111.blog.51cto.com/738881/163354 继上篇在Eclipse中搭好了tomcat环境后,我们建立一个最简单的servlet程序,这个serve ...

  6. c# 之五行地支

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. 周赛-Colored Sticks 分类: 比赛 2015-08-02 09:33 7人阅读 评论(0) 收藏

    Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 32423 Accepted: 8556 Desc ...

  8. 【20160924】GOCVHelper MFC增强算法(5)

    CString ExportListToExcel(CString  sExcelFile,CListCtrl* pList, CString strTitle)     {         CStr ...

  9. uva 816 Abbott的复仇

    题目链接:https://uva.onlinejudge.org/external/8/816.pdf 紫书:P165 题意: 有一个最多包含9*9个交叉点的迷宫.输入起点.离开起点时的朝向和终点,求 ...

  10. linux设备驱动编写_tasklet机制

    在编写设备驱动时, tasklet 机制是一种比较常见的机制,通常用于减少中断处理的时间,将本应该是在中断服务程序中完成的任务转化成软中断完成. 为了最大程度的避免中断处理时间过长而导致中断丢失,有时 ...