package com.example.day02_file;import java.util.Map;import com.example.lession02_file.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 static FileService fileService = null;// 声明获取得用户与密码的组件public EditText edit_name, edit_pass;// 声明登陆按钮对象public Button btn_login;// 声明CheckBox组件对象public CheckBox box_remember;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 设置显示视图setContentView(R.layout.activity_login);// 实例化业务对象fileService = new FileService(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(new MyOnClickListener());// 回显数据Map<String, String> map = fileService.readFile("private.txt");if (map != null) {edit_name.setText(map.get("edit_name"));edit_pass.setText(map.get("edit_pass"));}}@Overridepublic 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 {@Overridepublic 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()) {// 进行保存// 调用业务对象的业务方法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();}}}}}}
package com.example.lession02_file.service;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.HashMap;import java.util.Map;import com.example.lession02_file.util.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 */public boolean saveToRom(String name, String pass, String fileName) {// 上下文对象的apitry {// 通过openFileOutput()方法获取一个文件的输出流对象FileOutputStream fos = context.openFileOutput(fileName,Context.MODE_PRIVATE);// 拼接用户名与密码String result = name + ":" + pass;// 写入fos.write(result.getBytes());fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();return false;}return true;}// 读取数据public Map<String, String> readFile(String fileName) {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 (Exception e) {e.printStackTrace();}return map;}}
package com.example.lession02_file.util;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;public class StreamTools {public static String getValue(FileInputStream fis) throws Exception {//字节的输出流对象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;}}
<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/file_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/file_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" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_login" /> <CheckBox android:id="@+id/file_Chickbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/file_Chickbox" /> </LinearLayout></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">day02_file</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="file_name">用户名</string> <string name="file_pass">密 码</string> <string name="btn_login">登 陆</string> <string name="file_Chickbox">保存密码</string> <string name="file_text1"></string> <string name="file_text2"></string></resources>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.day02_file" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.day02_file.LoginActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>