2012-02-21 14 views
14

「ユーザーIDとパスワードを記憶させる」チェックボックスボタンがあります。誰もそれを開始する方法について正しい方向に私を導くことができますか?「私を覚えています」チェックボックスを追加してください

基本的にここで重要なのはSharedPreferencesです:

+0

あなたはすでに私を覚えていた機能を除いて、完全なログイン機能を構築することがありますか? – WarrenFaith

答えて

58

私はちょうど私のアプリにこれを建て、ここでは基本的なコードといくつかの説明があります。 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

David Davidさんのご協力ありがとうございました!理解するのに約40分かかりました。私はそのビットを稼いで今稼働しています:) –

+1

完璧に動作します!ありがとう.. – TharakaNirmana

+1

私はちょうど私のアプリのためのいくつかのコードをトーキング、正常に働いた! – Bachask8

関連する問題