java 安卓项目案例_Android开发登陆案例

这篇博客详细介绍了在Android应用中实现登录功能的步骤,包括布局设计、获取用户输入、判断输入有效性、保存用户信息到本地。通过示例代码展示了如何使用EditText、CheckBox、Button等组件,以及如何利用UserInfoUtil工具类进行文件操作来实现记住密码功能。
摘要由CSDN通过智能技术生成

eb78b70ef1e23cee5dde1fdb6ce12d90.png

layout

-

-

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 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 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 hashMap = new HashMap();

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 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 hashMap = new HashMap();

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开发登陆案例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值