邮箱:weimingwecom@sian.com
布局的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 用户名的布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_name" />
<EditText
android:id="@+id/edit_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
</LinearLayout>
<!-- 密码布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/view_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_pass" />
<EditText
android:id="@+id/edit_pass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword">
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_login"
android:layout_marginLeft="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_login" />
<CheckBox
android:id="@+id/cbx_rember"
android:layout_marginLeft="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_rember" />
</LinearLayout>
</LinearLayout>
Value里面的代码:<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">lession02-file</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="text_name">用户名:</string>
<string name="text_pass">密码:</string>
<string name="text_login">登陆</string>
<string name="text_rember">记住密码</string>
</resources>
效果图:
logActivi中的代码:
package com.example.file;
import java.io.IOException;
import java.util.Map;
import www.csdn.net.service.FileService;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity {
// 声明 获取的用户名与密码的组件
public EditText edit_name, edit_pass;
// 声明登陆按钮对象
public Button btn_login;
// 声明CheckBox组件对象
public CheckBox box_remember;
//创建业务对象
public FileService fileService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置显示视图
setContentView(R.layout.activity_login);
//实例化业务对象
fileService = new FileService(this);
// 根据id名称获取相应组件对象
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.cbx_rember);
// 给按钮注册事件
btn_login.setOnClickListener(new MyOnClickListener());
//回显数据
Map<String, String> map = null;
try {
map = fileService.readFile("private.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(map!=null){
edit_name.setText(map.get("name"));
edit_pass.setText(map.get("pass"));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
// 内部类
class MyOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
int id = v.getId();
// 判断当前点击组件是否是 按钮
if (id == btn_login.getId()) {
// 获取用户名与密码
String name = edit_name.getText().toString();
String pass = 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()) {
// 进行保存
//调用业务对象的业务方法
try {
LoginActivity.this.fileService.saveToRom(name, pass, "private.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(LoginActivity.this, "用户名和密码需要保存",
Toast.LENGTH_LONG).show();
} else {
// 不保存
Toast.makeText(LoginActivity.this, "用户名和密码不需要保存",
Toast.LENGTH_LONG).show();
}
}
}
}
}
}
Service;
package www.csdn.net.service;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import www.csdn.net.tools.StreamTools;
import android.content.Context;
public class FileService {
// 上下文的对象
public Context context;
public FileService(Context context) {
this.context = context;
}
/**
* 往手机内存上存储用户名与密码的操作
* @param name
* @param pass
* @param fileName
* @return
* @throws IOException
*/
public boolean saveToRom(String name,String pass,String fileName) throws IOException{
try {
// 通过openFileOutput()方法获取一个文件的输出流对象
FileOutputStream fos = context.openFileOutput(fileName,Context.MODE_PRIVATE);
// 拼接用户名与密码
String result = name+":"+pass;
fos.write(result.getBytes());
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
return true;
}
//读取数据
public Map<String,String> readFile(String fileName) throws IOException{
Map<String,String> map=null;// new HashMap<String, String>();
try {
FileInputStream fis = context.openFileInput(fileName);
String value = StreamTools.getValue(fis);
String values[] = value.split(":");
if(values.length>0){
map = new HashMap<String, String>();
map.put("name", values[0]);
map.put("pass", values[1]);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
}
调用的一个工具:
package www.csdn.net.tools;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class StreamTools {
public static String getValue(FileInputStream fis) throws IOException{
//字节的输出流对象
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
int length=1;
while ((length = fis.read(buffer)) != -1) {
stream.write(buffer, 0, length);
}
stream.flush();
stream.close();
String value= stream.toString();
return value;
}
}
在工里面 Window里面找到File Explorer 显示到控制台:
data- data-com.example-files- private.txt
显示的数局:aaa 123