[java]
packagecom.example.day02_file;
importjava.util.Map;
importcom.example.lession02_file.service.FileService;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.text.TextUtils;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.CheckBox;
importandroid.widget.EditText;
importandroid.widget.Toast;
publicclassLoginActivityextendsActivity{
publicstaticFileServicefileService=null;
//声明获取得用户与密码的组件
publicEditTextedit_name,edit_pass;
//声明登陆按钮对象
publicButtonbtn_login;
//声明CheckBox组件对象
publicCheckBoxbox_remember;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
//设置显示视图
setContentView(R.layout.activity_login);
//实例化业务对象
fileService=newFileService(this);
edit_name=(EditText)findViewById(R.id.edit_name);
edit_pass=(EditText)findViewById(R.id.edit_pass);
btn_login=(Button)findViewById(R.id.btn_login);
box_remember=(CheckBox)findViewById(R.id.file_Chickbox);
btn_login.setOnClickListener(newMyOnClickListener());
//回显数据
Mapmap=fileService.readFile("private.txt");
if(map!
=null){
edit_name.setText(map.get("edit_name"));
edit_pass.setText(map.get("edit_pass"));
}
}
@Override
publicbooleanonCreateOptionsMenu(Menumenu){
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate(R.menu.login,menu);
returntrue;
}
//内部类
classMyOnClickListenerimplementsView.OnClickListener{
@Override
publicvoidonClick(Viewv){
intid=v.getId();
if(id==btn_login.getId()){
Stringname=edit_name.getText().toString();
Stringpass=edit_pass.getText().toString();
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pass)){
Toast.makeText(LoginActivity.this,"用户名或密码不能为空",
Toast.LENGTH_LONG).show();
return;
}else{
//如果记住密码勾选上了
if(box_remember.isChecked()){
//进行保存
//调用业务对象的业务方法
LoginActivity.this.fileService.saveToRom(name,pass,
"private.txt");
Toast.makeText(LoginActivity.this,"用户名和密码需要保存",
Toast.LENGTH_LONG).show();
}else{
//不保存
Toast.makeText(LoginActivity.this,"用户名和密码不需要保存",
Toast.LENGTH_LONG).show();
}
}
}
}
}
}
packagecom.example.day02_file;
importjava.util.Map;
importcom.example.lession02_file.service.FileService;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.text.TextUtils;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.CheckBox;
importandroid.widget.EditText;
importandroid.widget.Toast;
publicclassLoginActivityextendsActivity{
publicstaticFileServicefileService=null;
//声明获取得用户与密码的组件
publicEditTextedit_name,edit_pass;
//声明登陆按钮对象
publicButtonbtn_login;
//声明CheckBox组件对象
publicCheckBoxbox_remember;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
//设置显示视图
setContentView(R.layout.activity_login);
//实例化业务对象
fileService=newFileService(this);
edit_name=(EditText)findViewById(R.id.edit_name);
edit_pass=(EditText)findViewById(R.id.edit_pass);
btn_login=(Button)findViewById(R.id.btn_login);
box_remember=(CheckBox)findViewById(R.id.file_Chickbox);
btn_login.setOnClickListener(newMyOnClickListener());
//回显数据
Mapmap=fileService.readFile("private.txt");
if(map!
=null){
edit_name.setText(map.get("edit_name"));
edit_pass.setText(map.get("edit_pass"));
}
}
@Override
publicbooleanonCreateOptionsMenu(Menumenu){
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate(R.menu.login,menu);
returntrue;
}
//内部类
classMyOnClickListenerimplementsView.OnClickListener{
@Override
publicvoidonClick(Viewv){
intid=v.getId();
if(id==btn_login.getId()){
Stringname=edit_name.getText().toString();
Stringpass=edit_pass.getText().toString();
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pass)){
Toast.makeText(LoginActivity.this,"用户名或密码不能为空",
Toast.LENGTH_LONG).show();
return;
}else{
//如果记住密码勾选上了
if(box_remember.isChecked()){
//进行保存
//调用业务对象的业务方法
LoginActivity.this.fileService.saveToRom(name,pass,
"private.txt");
Toast.makeText(LoginActivity.this,"用户名和密码需要保存",
Toast.LENGTH_LONG).show();
}else{
//不保存
Toast.makeText(LoginActivity.this,"用户名和密码不需要保存",
Toast.LENGTH_LONG).show();
}
}
}
}
}
}
[java]
[java]
[java]
packagecom.example.lession02_file.service;
importjava.io.ByteArrayOutputStream;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.util.HashMap;
importjava.util.Map;
importcom.example.lession02_file.util.StreamTools;
importandroid.content.Context;
publicclassFileService{
//上下文的对象
publicContextcontext;
publicFileService(Contextcontext){
this.context=context;
}
/**
*往手机内存上存储用户名与密码的操作
*
*@paramname
*@parampass
*@paramfileName
*@return
*/
publicbooleansaveToRom(Stringname,Stringpass,StringfileName){
//上下文对象的api
try{
//通过openFileOutput()方法获取一个文件的输出流对象
FileOutputStreamfos=context.openFileOutput(fileName,
Context.MODE_PRIVATE);
//拼接用户名与密码
Stringresult=name+":
"+pass;
//写入
fos.write(result.getBytes());
fos.flush();
fos.close();
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
returntrue;
}
//读取数据
publicMapreadFile(StringfileName){
Mapmap=null;//newHashMap();
try{
FileInputStreamfis=context.openFileInput(fileName);
Stringvalue=StreamTools.getValue(fis);
Stringvalues[]=value.split(":
");
if(values.length>0){
map=newHashMap();
map.put("name",values[0]);
map.put("pass",values[1]);
}
}catch(Exceptione){
e.printStackTrace();
}
returnmap;
}
}
packagecom.example.lession02_file.util;
importjava.io.ByteArrayOutputStream;
importjava.io.FileInputStream;
publicclassStreamTools{
publicstaticStringgetValue(FileInputStreamfis)throwsException{
//字节的输出流对象
ByteArrayOutputStreamstream=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlength=-1;
while((length=fis.read(buffer))!
=-1){
stream.write(buffer,0,length);
}
stream.flush();
stream.close();
Stringvalue=stream.toString();
returnvalue;
}
}
android="
xmlns:
tools="
android:
layout_width="match_parent"
android:
layout_height="match_parent"
android:
orientation="vertical">
android:
layout_width="match_parent"
android:
layout_height="wrap_content">
android:
id="@+id/View_name"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="@string/file_name"/>
android:
id="@+id/edit_name"
android:
layout_width="0dp"
android:
layout_height="wrap_content"
android:
layout_weight="1"
android:
ems="10"
android:
inputType="textPersonName">
android:
layout_width="match_parent"
android:
layout_height="wrap_content">
android:
id="@+id/View_pass"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="@string/file_pass"/>
android:
id="@+id/edit_pass"
android:
layout_width="0dp"
android:
layout_height="wrap_content"
android:
layout_weight="1"
android:
ems="10"
android:
inputType="textPassword"/>
android:
layout_width="match_parent"
android:
layout_height="wrap_content">
xmlversion="1.0"encoding="utf-8"?
>
day02_file
Settings
Helloworld!
用户名
密码
登陆
保存密码
xmlversion="1.0"encoding="utf-8"?
>
android="
package="com.example.day02_file"
android:
versionCode="1"
android:
versionName="1.0">
android:
minSdkVersion="8"
android:
targetSdkVersion="17"/>
android:
allowBackup="true"
android:
icon="@drawable/ic_launcher"
android:
label="@string/app_name"
android:
theme="@style/AppTheme">
android:
name="com.example.day02_file.LoginActivity"
android:
label="@string/