標籤:android 記住使用者名稱密碼 記憶體
xml檔案
<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" android:padding="5dp" > <EditText android:id="@+id/qqnum" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="請輸入QQ" android:inputType="number" android:textSize="20dp" /> <EditText android:id="@+id/pass" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="請輸入密碼" android:inputType="textPassword" android:textSize="20dp" /> <CheckBox android:id="@+id/rem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="記住密碼" /> <Button android:id="@+id/Login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登入"/></LinearLayout>
Utils
package com.example.android22filelogin;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import android.text.TextUtils;public class Utils {public static boolean saveUserInfo(String username,String pwd){String data=username+"##"+pwd;String path="/data/data/com.example.android22filelogin/data.txt";try {FileOutputStream out=new FileOutputStream(path);out.write(data.getBytes());out.flush();out.close();return true;} catch (Exception e) {e.printStackTrace();}return false;}public static Map<String,String> getUserInfo(){String path="/data/data/com.example.android22filelogin/data.txt";try {BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(path)));String data=reader.readLine();if(!TextUtils.isEmpty(data)){String [] datas=data.split("##");Map<String, String> userinfo=new HashMap<String, String>();userinfo.put("number", datas[0]);userinfo.put("pwd", datas[1]);return userinfo;}} catch (Exception e) {e.printStackTrace();}return null;}}
activity
public class MainActivity extends Activity implements OnClickListener {private EditText qqnum,pwd;private CheckBox rem;private Button but; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); qqnum=(EditText)findViewById(R.id.qqnum); pwd=(EditText)findViewById(R.id.pass); rem=(CheckBox)findViewById(R.id.rem); but=(Button)findViewById(R.id.Login); but.setOnClickListener(this); //回顯資料 Map<String, String> userinfo=Utils.getUserInfo(); if(userinfo!=null) { qqnum.setText(userinfo.get("number")); pwd.setText(userinfo.get("pwd")); } }@Overridepublic void onClick(View v) {//記住號碼和密碼String num=qqnum.getText().toString();String password=pwd.getText().toString();if(TextUtils.isEmpty(num)||TextUtils.isEmpty(password)){Toast.makeText(this, "使用者名稱或密碼不可為空", Toast.LENGTH_LONG).show();return;}//判斷是否記住密碼if(rem.isChecked()){boolean isSuccess=Utils.saveUserInfo(num, password);Toast.makeText(this, isSuccess+"", Toast.LENGTH_LONG).show();}//登入成功}}
本文出自 “Java大白的戰地” 部落格,請務必保留此出處http://8023java.blog.51cto.com/10117207/1664699
Android儲存資料到本地檔案