我們生活中會遇到各種各樣的登錄界面,需要輸入你的賬號和密碼,像登陸QQ呀,各種軟件都需要這種設計,那我們今天來實現一下吧~~
首先,界面的設計如下:

可以看出,我們需要兩個可編輯文本框,用來輸入用戶名和密碼,同時,我們要提示出要輸入的類型,用hint來提示;我們需要一個checkbox來判斷我們是否選擇保存密碼,還需要一個登陸button;由於整個線性布局是垂直的,因此兩個可編輯文本框,一個checkbox,一個button在垂直方向上依次排列,這樣不是很美觀,因此我們需要再建立一個相對布局,使checkbox與button排列在一行。具體實現如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.administrator.login.login" tools:showIn="@layout/activity_login"> <EditText android:id="@+id/mUserName" android:hint="用戶名" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:layout_below="@+id/mUserName" android:id="@+id/mPassWord" android:hint="密碼" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <CheckBox android:layout_centerVertical="true" android:id="@+id/mCheckBox" android:text="是否保存密碼" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/mButton" android:text="登陸" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> </LinearLayout>
設計完界面之后,我們來實現Activity:
實現用戶登錄界面需要幾個步驟:
1、因為Activity一啟動就會調用onCreate()函數,因此在onCreate()函數中獲取界面中的各個控件,通過findViewById()函數。
2、為button設置一個監聽器,當點下button並勾選checkbox時mCheckbox.isChecked(),獲取用戶名與密碼,通過輸出流輸出到內部存儲的info.txt文件中,為了方便讀取,我們設置##來分割用戶名與密碼。
3、不管有無勾選checkbox,只有點擊了button,我們就生成吐司提示界面,參數依次為:上下文環境(即當前Activity),要輸出的字符串以及輸出時間(3s或5s)
4、保存密碼功能實現的是當用戶退出程序,再一次進入時,已顯示出用戶名與密碼,我們通過readfrominfo()方法進行實現。
5、readfrominfo()中,為了方便對數據的操作,我們首先要把文件字節流轉化成輸入流再轉化為字符流,這樣我們就可以調用字符流的readline()函數(即實現按行讀取),若文件有多行,則設置循環while(下一行不為空){執行readline()},這樣我們就把info.txt中的信息讀到了字符串中了,我們把字符串中按##分割的各部分存入字符串數組中,則數組[0]為用戶名,數組[1]為密碼。
6、因為我們在一進入軟件就會執行onCreate()函數,所以在這里面我們就要調用readfrominfo()函數,但是,如果用戶是第一次登陸,並沒有生成info.txt,直接調用readfrominfo()函數肯定會出錯,因此在readfrominfo()函數中,我們要進行判斷,判斷info.txt文件是否存在file.exits()
代碼如下:
package com.administrator.login; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; public class login extends AppCompatActivity { private EditText mUsername; private EditText mPassWord; String username=null; String password=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); Button mButton=(Button)findViewById(R.id.mButton); mUsername=(EditText)findViewById(R.id.mUserName); mPassWord=(EditText)findViewById(R.id.mPassWord); final CheckBox mCheckbox=(CheckBox)findViewById(R.id.mCheckBox); readfrominfo(); //調用讀內部存儲函數 mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mCheckbox.isChecked()){ username=mUsername.getText().toString(); password=mPassWord.getText().toString(); File file = new File(getFilesDir(),"info.txt"); //寫內部存儲到info.txt文件 FileOutputStream fos= null; //創建輸出流 try { fos = new FileOutputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } try { fos.write((username+"##"+password).getBytes()); //將username##password按位寫入文件中 fos.close(); //關閉流 } catch (IOException e) { e.printStackTrace(); } } Toast.makeText(login.this,"登陸成功",Toast.LENGTH_SHORT).show(); //吐司界面,參數依次為提示發出Activity,提示內容,提示時長 } }); } //讀內部存儲函數 public void readfrominfo(){ File file = new File(getFilesDir(),"info.txt"); if(file.exists()){ BufferedReader br= null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); //將字節流轉化為輸入流然后轉化為字符流 } catch (FileNotFoundException e) { e.printStackTrace(); } String str = null; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } String [] up= str.split("##"); //將字符串按##分割的部分依次存入字符串型數組up中 mUsername.setText(up[0]); //填寫username與password mPassWord.setText(up[1]); } } }
輸出結果:

