当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发登陆案例

Android开发登陆案例

2019年07月24日  | 移动技术网IT编程  | 我要评论

星尚人气美食,gummy 雪花,www.9ku

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开发登陆案例,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网