今天在用某記賬軟件,我心里就再琢磨著,萬一被老婆拿到手機,胡亂翻一通,萬一看到了我的用錢流水賬,那可不好,要遭!我要隱私~怎么辦呢?于是發(fā)現(xiàn)其實人家早已想到,為用戶考慮到了這個問題,有個設置密碼功能,并且我發(fā)現(xiàn)啟動后,輸入密碼連以往的登錄或者進入進入的按鈕都沒有省去了,只要密碼輸入完匹配成功就自動進入了,好神奇,什么樣做的呢?這個設計,能減少用戶輸入,同時還有心思的設計是就算在沒有輸入密碼的時候也可以進行收入和支出的記錄,只是看不到詳細內(nèi)容,非常好的設計!花了一點時間思考了一下就實現(xiàn)了這個小巧的自動登錄功能。
下面把代碼貼上來吧,大蝦就勿噴了!
LoginActivity.java
[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();
}
}
跳轉(zhuǎn)過去的second.activity各位就隨便弄一個吧!不過需要記住的就是新增了activity后記著要在AndroidManifest.xml中添加這個activity托福答案
mian.xml
[html]
<merge xmlns:android="schemas.android.com/apk/res/android"
xmlns:tools="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的一些監(jiān)聽的函數(shù)了,例如還有一些自動補全特別是用于賬號輸入的時候,這里主要就是用到了監(jiān)聽edittext的變化用到了addTextChangedListener,并且配合TextWatcher()就能實現(xiàn)在輸入的過程中進行一些操作了,把登錄跳轉(zhuǎn)的方法寫在了onTextChanged里北美托福答案
好了,我這里預設的密碼就是123456設置死了的,大家可以試一試啦!
延伸問題:
1.這款記賬軟件設置的密碼就是保存在本地的,那么如何保存,讀取這個設置的密碼呢?(這個其實就設計到了本地數(shù)據(jù)的存儲,方法也多種多樣,后面有時間我們接著討論這個托福改分
希望和大家多多交流,我也是android新兵,希望我們在這條路上堅持走下去,哪怕是一個小小的功能,只要能方便用戶,就會為我們增加更多的使用者和用戶。大家加油,積少成多,集腋成裘雅思答案