登陸界面的數據保存回顯的操作

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>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章