我想要一個複選框按鈕來記住用戶名和密碼。任何人都可以請指導我在正確的方向如何讓它開始?添加一個「記住我」複選框
回答
我剛建到我的應用程序這一點,這裏的基本代碼和一些說明:
基本上這裏的關鍵是SharedPreferences。您將設置一個SharedPreferences對象並在用戶輸入用戶名和密碼後存儲您的用戶名和密碼。然後,當他們再次運行應用程序時,首選項將存儲其數據,然後重新填充登錄字段。
LoginActivity.java
package com.realsimpleapps.LoginTesting;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class LoginActivity extends Activity implements OnClickListener {
private String username,password;
private Button ok;
private EditText editTextUsername,editTextPassword;
private CheckBox saveLoginCheckBox;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private Boolean saveLogin;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
ok = (Button)findViewById(R.id.buttonLogin);
ok.setOnClickListener(this);
editTextUsername = (EditText)findViewById(R.id.editTextUsername);
editTextPassword = (EditText)findViewById(R.id.editTextPassword);
saveLoginCheckBox = (CheckBox)findViewById(R.id.saveLoginCheckBox);
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
saveLogin = loginPreferences.getBoolean("saveLogin", false);
if (saveLogin == true) {
editTextUsername.setText(loginPreferences.getString("username", ""));
editTextPassword.setText(loginPreferences.getString("password", ""));
saveLoginCheckBox.setChecked(true);
}
}
public void onClick(View view) {
if (view == ok) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextUsername.getWindowToken(), 0);
username = editTextUsername.getText().toString();
password = editTextPassword.getText().toString();
if (saveLoginCheckBox.isChecked()) {
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor.putString("username", username);
loginPrefsEditor.putString("password", password);
loginPrefsEditor.commit();
} else {
loginPrefsEditor.clear();
loginPrefsEditor.commit();
}
doSomethingElse();
}
}
public void doSomethingElse() {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
LoginActivity.this.finish();
}
}
該方法在年底,doSomethingElse()是你的佔位符去爲你的應用程序的下一個步驟。我的doSomethingElse()方法只是加載另一個活動。
下面是登錄頁面的基本XML文件:
login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
android:padding="10dp" >
<EditText
android:id="@+id/editTextUsername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:hint="Username"
android:inputType="textNoSuggestions"
android:imeOptions="actionNext" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editTextUsername"
android:hint="Password"
android:inputType="textPassword"
android:imeOptions="actionDone" />
<CheckBox
android:id="@+id/saveLoginCheckBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editTextPassword"
android:text="Save Login?"
android:textColor="#FFF" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/saveLoginCheckBox"
android:layout_marginTop="40dp"
android:text="Login" />
</RelativeLayout>
重要提示:您可能會希望將其存儲在SharedPreferences之前加密密碼。有關詳細信息超出了此問題的範圍,但以下是我用來執行此操作的代碼:http://www.androidsnippets.com/encryptdecrypt-strings。你也必須想出一些關鍵模式。
此代碼已在Android 2.1 SDK 7上測試過。請讓我知道它如何適用於您。
大衛
非常感謝大家的幫助!我花了大約40分鐘才明白。我得到了一點點,現在運行:) – 2012-02-21 17:55:40
完美的作品!謝謝.. – TharakaNirmana 2013-07-10 06:17:21
我剛剛爲我的應用程序添加了一些代碼,工作正常! – Bachask8 2013-10-19 19:45:07
- 1. Android登錄記住我複選框
- 2. 彈簧安全「記住我」複選框
- 3. 記住複選框的值
- 4. 記住與localstorage複選框onclick
- 5. 如何記住複選框的位置?
- 6. Box v2 API - 「記住登錄」複選框
- 7. 記住Smarty引擎中的複選框
- 8. 記住ExtJS的4複選框
- 9. 記住表單複選框的值
- 10. 使用複選框記住Gridview狀態
- 11. 添加複選框項目onclick的另一個複選框
- 12. 添加一個複選框到cxGridDBColumn(DateEdit)
- 13. 添加一個複選框使用JQuery
- 14. 用iTextSharp添加一個複選框
- 15. 添加一個複選框,設置AlertDialog
- 16. 一個複選框列添加到GridView
- 17. 我如何添加一個複選框到這個PHP表格
- 18. 記住會話中的每個複選框選擇
- 19. ASP.NET MVC新手 - 默認登錄頁面記住我複選框
- 20. 將'記住我'複選框轉到提交
- 21. 是OpenID的「記住我」複選框開發人員使用?
- 22. Rails,HAML記住我複選框不工作
- 23. 刪除記住我從asp複選框:登錄控制
- 24. 當我添加一個字段集時,複選框消失
- 25. 如何在我的gridview中添加一個複選框?
- 26. 添加一個複選框不添加開始菜單圖標
- 27. 記住選項框的值
- 28. 了JavaScript複選框被選中,添加複選框ID到一個數組
- 29. 如何添加一個複選標記到列表框中的選定項目
- 30. 選擇標記卡住一個選項
您是否已經構建了除記住我功能之外的完整登錄功能? – WarrenFaith 2012-02-21 00:12:23