2012-02-21 69 views

回答

58

我剛建到我的應用程序這一點,這裏的基本代碼和一些說明:

基本上這裏的關鍵是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上測試過。請讓我知道它如何適用於您。

大衛

+1

非常感謝大家的幫助!我花了大約40分鐘才明白。我得到了一點點,現在運行:) – 2012-02-21 17:55:40

+1

完美的作品!謝謝.. – TharakaNirmana 2013-07-10 06:17:21

+1

我剛剛爲我的應用程序添加了一些代碼,工作正常! – Bachask8 2013-10-19 19:45:07