Android开发登陆案例
作者:Qi_Yuan 发布时间:2022-03-19 10:13:01
layout
<?xml version="1.0"?>
-<LinearLayout android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<EditText android:id="@+id/et_username" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="@string/input_username"/>
<EditText android:id="@+id/et_password" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="@string/input_password" android:inputType="textPassword" android:layout_marginBottom="10dp" android:layout_marginTop="10dp"/>
-<RelativeLayout android:layout_height="wrap_content" android:layout_width="fill_parent">
<CheckBox android:id="@+id/cb_rem" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/rem_password" android:layout_alignParentLeft="true" android:layout_centerVertical="true"/>
<Button android:id="@+id/bt_login" android:paddingRight="50dp" android:paddingLeft="50dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/login" android:layout_centerVertical="true" android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
java代码
package com.itheima.login;
import java.util.Map;
import com.itheima.login.util.UserInfoUtil;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private EditText et_username;
private EditText et_password;
private CheckBox cb_rem;
private Button bt_login;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
cb_rem = (CheckBox) findViewById(R.id.cb_rem);
bt_login = (Button) findViewById(R.id.bt_login);
//b.设置按钮的点击事件
bt_login.setOnClickListener(this);
//f.回显用户名密码 ??
Map<String, String> map = UserInfoUtil.getUserInfo_android(mContext);//获取用户名密码
if(map != null){
String username = map.get("username");
String password = map.get("password");
et_username.setText(username);//设置用户名
et_password.setText(password);
cb_rem.setChecked(true);//设置复选框选中状态
}
}
private void login(){
//c.在onclick方法中,获取用户输入的用户名密码和是否记住密码
String username = et_username.getText().toString().trim();
String password = et_password.getText().toString().trim();
boolean isrem = cb_rem.isChecked();
//d.判断用户名密码是否为空,不为空请求服务器(省略,默认请求成功)
if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
Toast.makeText(mContext, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
return ;
}
//请求服务器,后面讲。。。。。。。。。。
//e.判断是否记住密码,如果记住,将用户名密码保存本地。????
if(isrem){
boolean result = UserInfoUtil.saveUserInfo_android(mContext,username,password);
if(result){
Toast.makeText(mContext, "用户名密码保存成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext, "用户名密码保存失败", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(mContext, "无需保存", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
login();
break;
default:
break;
}
}
}
新建包的代码
package com.itheima.login.util;
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;
import android.content.Context;
public class UserInfoUtil {
//保存用户名密码
public static boolean saveUserInfo_android(Context context,String username, String password) {
try{
String userinfo = username + "##"+ password;//封装用户名密码
//得到私有目录下一个文件写入流; name : 私有目录文件的名称 mode: 文件的操作模式, 私有,追加,全局读,全局写
FileOutputStream fileOutputStream = context.openFileOutput("userinfo.txt", Context.MODE_PRIVATE);
fileOutputStream.write(userinfo.getBytes());//将用户名密码写入文件
fileOutputStream.close();
return true;
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
//获取用户名密码
public static Map<String ,String> getUserInfo_android(Context context){
try{
//通过context对象获取一个私有目录的文件读取流
FileInputStream fileInputStream = context.openFileInput("userinfo.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
//读取一行中包含用户密码,需要解析
String readLine = bufferedReader.readLine();
String[] split = readLine.split("##");
HashMap<String, String> hashMap = new HashMap<String ,String>();
hashMap.put("username", split[0]);
hashMap.put("password", split[1]);
bufferedReader.close();
fileInputStream.close();
return hashMap;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
//保存用户名密码
public static boolean saveUserInfo(Context context,String username, String password) {
try{
String userinfo = username + "##"+ password;//封装用户名密码
// String path = "/data/data/com.itheima.login/";//指定保存的路径
//通过Context对象获取私有目录的一个路径
String path = context.getFilesDir().getPath();
System.out.println("...............:"+path);
File file = new File(path,"userinfo.txt");//创建file
FileOutputStream fileOutputStream = new FileOutputStream(file);//创建文件写入流
fileOutputStream.write(userinfo.getBytes());//将用户名密码写入文件
fileOutputStream.close();
return true;
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
//获取用户名密码
public static Map<String ,String> getUserInfo(Context context){
try{
// String path = "/data/data/com.itheima.login/";//指定保存的路径
//通过Context对象获取私有目录的一个路径
String path = context.getFilesDir().getPath();
System.out.println("...............:"+path);
File file = new File(path,"userinfo.txt");//创建file
FileInputStream fileInputStream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
//读取一行中包含用户密码,需要解析
String readLine = bufferedReader.readLine();
String[] split = readLine.split("##");
HashMap<String, String> hashMap = new HashMap<String ,String>();
hashMap.put("username", split[0]);
hashMap.put("password", split[1]);
bufferedReader.close();
fileInputStream.close();
return hashMap;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
我存在的问题与修正
*alt+enter------补全抽象方法*/
/*获取全局变量*****怎么做 fied*/
/*如何格式化代码*/
/*点击事件用全局语句*/
/*很多程序都用到contest,所以在类中定义对象吧!赋值this,以后toast用到直接调用*/
/*ctrl+1 封装临时自己打的类,crate class*/
/*创建方法*/
/*保存文件*/
1.保存到私有目录下
2.保存路径
3.创建一个File对象
4.再创建一个FileOotputStream对象,传File进去
/*私开包,斯开方法*/
/*保存文件*/
1.保存到私有目录下 /date/date/包名/
2.保存路径(特殊)
3.创建一个File对象(路径,文件类型加名字)
4.再创建一个FileOotputStream对象,传File进去,其实就是创建文件写入流
5.对读取这一块直接 try一下 catch输出信息(什么stake)
6.FS调用write方法,传字节流进去。传字节进去,而且自能传一个,怎么办?
用字符串+ 处理 那混合了怎么办?
加两个特殊字符进去##(不能用正则表达式的字符)。后面再用 分割字符串的方法分开
7.字符串调用自身 getbyte()方法
8.把流关闭 FS调用close()方法
9.最后return ture 告诉保存成功
/*Map?*/
/*toast*/
1.Toast.makeText(mtext,"Stri ng",Toast.选时间).show
2.mcontext=this ,就是创建一个数据
/*什么时候回显*/
1.程序一加载就回显示
2.是不是要读取文件才能读取
3.读的路径一样,创建的文件一样
4.创建一个输入字节流 FIS(F)
4.用户名,密码都是一行,怎么读取一行
创建BR对象(new IR(F))
5.创建字符串读取字节 BR。RL()
6.分割字符串的使用
7.集合的使用 哈希表
8.关闭流
以上所述是小编给大家介绍的Android开发登陆案例网站的支持!
来源:http://www.cnblogs.com/liangqiyuan/archive/2016/07/19/5683891.html


猜你喜欢
- RestTemplate 是由 Spring 提供的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、PO
- 前言这似乎是 Reactor 的热门搜索之一,至少当我在谷歌中输入 onErrorContinue 时,onErrorResume 会在它旁
- 为什么要有线程池?在实际使用中,服务器在创建和销毁线程上花费的时间和消耗的系统资源都相当大,所以要尽可能减少创建和销毁线程的次数。由于没有线
- 这篇文章主要介绍了Java编码摘要算法实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参
- 假定创建了XmlWriter的实例变量xmlWriter,下文中将使用此实例变量写Xml1.如何使用XmlWriter写Xml文档声明//
- 一、简介线程安全概念:线程安全是指在当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用。不会出
- 一、SpringBoot是什么Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以
- public class MainActivity extends Activity { TextView tv; Ch
- 事件(event),这个词儿对于初学者来说,往往总是显得有些神秘,不易弄懂。而这些东西却往往又是编程中常用且非常重要的东西。大家都知道win
- Filter学习Filter功能拦截jsp、静态图片文件、静态html资源文件实现URL级别的权限访问控制过滤敏感词汇压缩相应信息Filte
- 本文是项目中使用了websocket进行一些数据的推送,对比项目做了一个demo,ws的相关问题不做细数,仅做一下记录。此demo针对ws的
- 传值就是将实参的值传到所调用的函数里面,实参的值并没有发生变化,默认传值的有int型,浮点型,bo
- 什么是线程线程被称为轻量级进程,是程序执行的最小单位,它是指在程序执行过程中,能够执行代码的一个执行单位。每个程序程序都至少有一个线程,也即
- ELK是三款软件的简称,分别是Elasticsearch、Logstash、Kibana组成,在发展的过程中,又有新成员Beats的加入,所
- 启动命令:java -jar weichi-1.0.0.jar将命令打印到1.log上 java -jar weichi-1.0.0.jar
- Java中有四种权限修饰符publicprotected(default)private同一个类yesyesyesyes同一个包yesyes
- 初始化sp 内部将数据放到 xml 文件中,加载时首先会将硬盘中文件读取到内存中,这样加快了访问速度这次从源码开始,看看里面具体做了什么//
- 使用场景1、将用户信息导出为excel表格(导出数据....)2、将Excel表中的信息录入到网站数据库(习题上传....)大大减轻网站录入
- —举例(学生排课)—正常思路的处理方法和优化过后的处理方法:比如说给学生排课。学生和课程是一个多对多的关系。按照正常的逻辑 应该有一个关联表
- 下文笔者讲述maven引入本地jar包时,运行报错"java.lang.NoClassDefFoundError"的处理