密码对象登陆界面的数据保存回显的操作

最近使用开发的过程中出现了一个小问题,顺便记录一下原因和方法--密码对象

密码和对象

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;

	@Override
	protected 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"));

		}

	}

	@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()) {
						// 进行保存
						// 调用业务对象的业务方法
						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) {
		// 上下文对象的api
		try {
			// 通过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>

    

文章结束给大家分享下程序员的一些笑话语录: 这年头的互联网真是娱乐了中国,网民们从各种各样的“门”里钻来钻去,又有好多“哥”好多“帝”,值得大家品味不已……网络经典语录,关于IT与互联网,经典与您分享!

--------------------------------- 原创文章 By
密码和对象
---------------------------------

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3106937.html