今天在用某記賬軟件,我心裏就再琢磨着,萬一被老婆拿到手機,胡亂翻一通,萬一看到了我的用錢流水賬,那可不好,要遭!我要隱私~怎麼辦呢?於是發現其實人家早已想到,爲用戶考慮到了這個問題,有個設置密碼功能,並且我發現啓動後,輸入密碼連以往的登錄或者進入進入的按鈕都沒有省去了,只要密碼輸入完匹配成功就自動進入了,好神奇,什麼樣做的呢?這個設計,能減少用戶輸入,同時還有心思的設計是就算在沒有輸入密碼的時候也可以進行收入和支出的記錄,只是看不到詳細內容,非常好的設計!花了一點時間思考了一下就實現了這個小巧的自動登錄功能。
下面把代碼貼上來吧,大蝦就勿噴了!
LoginActivity.java
package com.challen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
/**
* Activity which displays a login screen to the user, offering registration as
* well.
*/
public class LoginActivity extends Activity {
private EditText mPasswordView;
private TextView mLoginStatusMessageView;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLoginStatusMessageView = (TextView)findViewById(R.id.sign_in_button);
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(mPasswordView.getText().toString().equals("123456")){
intent = new Intent(LoginActivity.this,secondActivity.class);
startActivity(intent);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
protected void onResume() {
// 由於我知道你會點擊返回鍵反覆試驗,所以加了這句話
mPasswordView.setText("");
super.onResume();
}
}
跳轉過去的second.activity各位就隨便弄一個吧!不過需要記住的就是新增了activity後記着要在AndroidManifest.xml中添加這個activity
mian.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginActivity" >
<!-- Login form -->
<LinearLayout
style="@style/LoginFormContainer"
android:orientation="vertical" >
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="輸入密碼"
android:maxLines="1"
android:singleLine="true" />
<TextView
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="16dp"
android:paddingLeft="32dp"
android:paddingRight="32dp"
android:text="密碼匹配成功則自動登錄" />
</LinearLayout>
</merge>
其實最關鍵的就是知道edittext的一些監聽的函數了,例如還有一些自動補全特別是用於賬號輸入的時候,這裏主要就是用到了監聽edittext的變化用到了addTextChangedListener,並且配合TextWatcher()就能實現在輸入的過程中進行一些操作了,把登錄跳轉的方法寫在了onTextChanged裏。
好了,我這裏預設的密碼就是123456設置死了的,大家可以試一試啦!
延伸問題:
1.這款記賬軟件設置的密碼就是保存在本地的,那麼如何保存,讀取這個設置的密碼呢?(這個其實就設計到了本地數據的存儲,方法也多種多樣,後面有時間我們接着討論這個)
希望和大家多多交流,我也是android新兵,希望我們在這條路上堅持走下去,哪怕是一個小小的功能,只要能方便用戶,就會爲我們增加更多的使用者和用戶。大家加油,積少成多,集腋成裘!