在安卓程序中,我們經常會使用到很多登陸的功能,會看到很多記住密碼的地方。這里,我給大家寫一個簡單的記住密碼的功能。注意,這里是簡單的記住密碼功能,僅用於練習學習用哦。這個程序主要是給學習安卓的朋友作為一個入門程序來學習。用到的知識點主要就是JAVA的IO流知識。所以,如果對IO流知識不熟悉的朋友們,一定要好好回去復習一下IO的知識。IO流的知識對於我們以后的開發有着十分重要的地位。這里順便給點建議,學習安卓其實到頭來,難得還是JAVA。如果前期JAVA學的好,其實安卓學起來是還是比較得心應手的。
這里,我們先說一下這個程序吧。
程序主要實現的功能就是,用戶輸入賬號和密碼,如果用戶有勾選記住密碼,點擊登陸按鈕后再退出程序,下一次啟動程序的時候,會自動加載賬號和密碼。如果用戶沒有勾選就不會加載。
原理:這里我們主要就是把用戶輸入的賬號和密碼用IO流寫入到info.txt文件中,如果有記住密碼,在下一次啟動的時候,就再用IO流去讀取info.txt中的賬號和密碼。
說到文件讀寫的問題,順便說一下,安卓中,程序只能對“data/data/自己程序文件夾”這個路徑下進行操作。其他程序下的文件夾是無法訪問的。
下面我們看看運行截圖和代碼。我們先看看截圖
下面看看代碼
布局文件 activity_main.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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.mylogin.MainActivity" > <EditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/tips" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/tips2" android:inputType="textPassword" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <CheckBox android:id="@+id/cb_remerber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="@string/tips3" /> <Button android:id="@+id/bt_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:onClick="login" android:text="@string/login" > </Button> </RelativeLayout> </LinearLayout>
字符串文件 strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">mylogin</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="tips">請輸入用戶名</string> <string name="tips2">請輸入用戶密碼</string> <string name="tips3">記住密碼</string> <string name="login">登陸</string> </resources>
Java文件MainActivity.java
package com.example.mylogin; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText userName, passWord; private CheckBox box; File file = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); userName = (EditText) findViewById(R.id.et_username); passWord = (EditText) findViewById(R.id.et_password); box = (CheckBox) findViewById(R.id.cb_remerber); try { load(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 點擊登陸,寫入賬戶密碼的方法 @SuppressLint("ShowToast") public void login(View v) throws IOException { String name = userName.getText().toString(); String pwd = passWord.getText().toString(); FileOutputStream fos = null; // 判斷是否有勾選記住密碼 if (box.isChecked()) { try { /* * getFilesDir()路徑其實就是data/data/項目包/files 安卓中,每一個程序只能在自己的包下進行讀寫。 * 例如,本例子中,其實路徑就是 data/data/com.examle.mylogin/files/info.txt
* 這里補充一點,如果文件要寫在sd卡上,那么路徑為storage/sdcard/info.txt,注意,寫在sd卡是要添加讀寫權限的。
* 當然咯,路徑不用自己寫,可以用api獲取,Environment.getExternalStorageDirectory()
* android.permission.READ_EXTERNAL_STORAGE,android.permission.WRITE_EXTERNAL_STORAGE */ file = new File(getFilesDir(), "info.txt"); fos = new FileOutputStream(file); // 將name和pwd轉化為字節數組寫入。##是為了方便待會分割 fos.write((name + "##" + pwd).getBytes()); Toast.makeText(MainActivity.this, "登陸成功", 0).show(); } finally { if (fos != null) { fos.close(); } } } else { // 如果用戶沒有勾選記住密碼,就判斷file是否存在,存在就刪除 if (file.exists()) { file.delete(); Toast.makeText(MainActivity.this, "登陸成功", 0).show(); } else { Toast.makeText(MainActivity.this, "登陸成功", 0).show(); } } } // 加載賬戶密碼的方法 public void load() throws IOException { FileInputStream fiStream = null; BufferedReader br = null; file = new File(getFilesDir(), "info.txt"); if (file.exists()) { try { fiStream = new FileInputStream(file); /* 將字節流轉化為字符流,轉化是因為我們知道info.txt * 只有一行數據,為了使用readLine()方法,所以我們這里 * 轉化為字符流,其實用字節流也是可以做的。但比較麻煩 */ br = new BufferedReader(new InputStreamReader(fiStream)); //讀取info.txt String str = br.readLine(); //分割info.txt里面的內容。這就是為什么寫入的時候要加入##的原因 String arr[] = str.split("##"); userName.setText(arr[0]); passWord.setText(arr[1]); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (br != null) { br.close(); } } } else { } } }