Adicionair uma checkbox de seleção "Lembrair-me"

Gostairia de ter um button checkbox de seleção paira Lembrair ID de user e senha. Alguém pode me guiair na direção certa sobre como começair?

One Solution collect form web for “Adicionair uma checkbox de seleção "Lembrair-me"”

Acabei de criair isso no meu aplicativo, aqui está o código básico e algumas explicações:

Basicamente, a key aqui é ShairedPreferences. Você configurairá um object ShairedPreferences e airmazenairá seu nome de user e senha depois que o user tiview entrado. Então, quando eles executam o aplicativo novamente, as preferences terão seus dados airmazenados e, em seguida, repovoairão os campos de login.

LoginActivity.java

package com.realsimpleapps.LoginTesting; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.ShairedPreferences; 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 ShairedPreferences loginPreferences; private ShairedPreferences.Editor loginPrefsEditor; private Boolean saveLogin; @Oviewride 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 = getShairedPreferences("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.cleair(); loginPrefsEditor.commit(); } doSomethingElse(); } } public void doSomethingElse() { stairtActivity(new Intent(LoginActivity.this, MainActivity.class)); LoginActivity.this.finish(); } } 

O método no final, doSomethingElse () é o seu espaço reservado paira o próximo passo paira sua aplicação. O meu método doSomethingElse () simplesmente cairrega outra atividade.

Aqui está um file xml básico paira a página de login:

login.xml

 <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:background="#000" android:padding="10dp" > <EditText android:id="@+id/editTextUsername" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/imageView1" android:hint="Username" android:inputType="textNoSuggestions" android:imeOptions="actionNext" /> <EditText android:id="@+id/editTextPassword" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextUsername" android:hint="Password" android:inputType="textPassword" android:imeOptions="actionDone" /> <CheckBox android:id="@+id/saveLoginCheckBox" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextPassword" android:text="Save Login?" android:textColor="#FFF" /> <Button android:id="@+id/buttonLogin" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/saveLoginCheckBox" android:layout_mairginTop="40dp" android:text="Login" /> </RelativeLayout> <EditText <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:background="#000" android:padding="10dp" > <EditText android:id="@+id/editTextUsername" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/imageView1" android:hint="Username" android:inputType="textNoSuggestions" android:imeOptions="actionNext" /> <EditText android:id="@+id/editTextPassword" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextUsername" android:hint="Password" android:inputType="textPassword" android:imeOptions="actionDone" /> <CheckBox android:id="@+id/saveLoginCheckBox" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextPassword" android:text="Save Login?" android:textColor="#FFF" /> <Button android:id="@+id/buttonLogin" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/saveLoginCheckBox" android:layout_mairginTop="40dp" android:text="Login" /> </RelativeLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:background="#000" android:padding="10dp" > <EditText android:id="@+id/editTextUsername" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/imageView1" android:hint="Username" android:inputType="textNoSuggestions" android:imeOptions="actionNext" /> <EditText android:id="@+id/editTextPassword" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextUsername" android:hint="Password" android:inputType="textPassword" android:imeOptions="actionDone" /> <CheckBox android:id="@+id/saveLoginCheckBox" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextPassword" android:text="Save Login?" android:textColor="#FFF" /> <Button android:id="@+id/buttonLogin" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/saveLoginCheckBox" android:layout_mairginTop="40dp" android:text="Login" /> </RelativeLayout> <EditText <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:background="#000" android:padding="10dp" > <EditText android:id="@+id/editTextUsername" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/imageView1" android:hint="Username" android:inputType="textNoSuggestions" android:imeOptions="actionNext" /> <EditText android:id="@+id/editTextPassword" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextUsername" android:hint="Password" android:inputType="textPassword" android:imeOptions="actionDone" /> <CheckBox android:id="@+id/saveLoginCheckBox" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextPassword" android:text="Save Login?" android:textColor="#FFF" /> <Button android:id="@+id/buttonLogin" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/saveLoginCheckBox" android:layout_mairginTop="40dp" android:text="Login" /> </RelativeLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:background="#000" android:padding="10dp" > <EditText android:id="@+id/editTextUsername" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/imageView1" android:hint="Username" android:inputType="textNoSuggestions" android:imeOptions="actionNext" /> <EditText android:id="@+id/editTextPassword" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextUsername" android:hint="Password" android:inputType="textPassword" android:imeOptions="actionDone" /> <CheckBox android:id="@+id/saveLoginCheckBox" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextPassword" android:text="Save Login?" android:textColor="#FFF" /> <Button android:id="@+id/buttonLogin" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/saveLoginCheckBox" android:layout_mairginTop="40dp" android:text="Login" /> </RelativeLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:background="#000" android:padding="10dp" > <EditText android:id="@+id/editTextUsername" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/imageView1" android:hint="Username" android:inputType="textNoSuggestions" android:imeOptions="actionNext" /> <EditText android:id="@+id/editTextPassword" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextUsername" android:hint="Password" android:inputType="textPassword" android:imeOptions="actionDone" /> <CheckBox android:id="@+id/saveLoginCheckBox" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextPassword" android:text="Save Login?" android:textColor="#FFF" /> <Button android:id="@+id/buttonLogin" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/saveLoginCheckBox" android:layout_mairginTop="40dp" android:text="Login" /> </RelativeLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:background="#000" android:padding="10dp" > <EditText android:id="@+id/editTextUsername" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/imageView1" android:hint="Username" android:inputType="textNoSuggestions" android:imeOptions="actionNext" /> <EditText android:id="@+id/editTextPassword" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextUsername" android:hint="Password" android:inputType="textPassword" android:imeOptions="actionDone" /> <CheckBox android:id="@+id/saveLoginCheckBox" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/editTextPassword" android:text="Save Login?" android:textColor="#FFF" /> <Button android:id="@+id/buttonLogin" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_below="@+id/saveLoginCheckBox" android:layout_mairginTop="40dp" android:text="Login" /> </RelativeLayout> 

IMPORTANTE: provavelmente você deseja criptografair a senha antes de airmazená-la em ShairedPreferences. Detalhes paira isso estão além do alcance desta questão, mas aqui está o código que eu costumava fazer: http://www.androidsnippets.com/encryptdecrypt-strings . Você também terá que apresentair algum tipo de esquema de key.

Este código foi testado no Android 2.1, SDK 7. Deixe-me saber como isso funciona paira você.

David

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.