·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> Android开发 >> Android开发登陆案例

Android开发登陆案例

作者:佚名      Android开发编辑:admin      更新时间:2022-07-23

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