標籤:
我們生活中會遇到各種各樣的登入介面,需要輸入你的帳號和密碼,像登陸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]); } }}
輸出結果:
Android——Login使用者登陸的設計實現