android SharedPreferences实现用户的注册和保存账号密码
生活随笔
收集整理的這篇文章主要介紹了
android SharedPreferences实现用户的注册和保存账号密码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在android開發中,我們做用戶登錄和注冊的時候需要將用戶名和密碼保存,下次打開的時候記住應戶名和密碼。關于注冊保存用戶密碼的方式和記住用戶名和密碼的保存方式有多種,這里介紹SharaedPreferenses保存方式。SharaedPreferenses是使用鍵值對的方式來存儲數據的,它的本質是基于XML文件存儲key-value鍵值對數據,通常用來存儲一些簡單的配置信息。其存儲位置在/data/data/<包名>/shared_prefs目錄下。
下面就通過一個小例子來實現這兩項功能
1、注冊的功能
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".RegisteActivity" ><TableLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:stretchColumns="1"><TableRow ><TextViewandroid:layout_height="wrap_content"android:text="賬號:"/><EditText android:id="@+id/account"android:layout_height="wrap_content"android:hint="請輸入您的賬號"/></TableRow><TableRow ><TextViewandroid:layout_height="wrap_content"android:text="密碼:"/><EditText android:id="@+id/passeord"android:layout_height="wrap_content"android:inputType="textPassword"/></TableRow></TableLayout><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="確定" /></LinearLayout>在這里使用TableLayout使布局更加合理 package com.example.sharepreferences;import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.nfc.Tag; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;public class RegisteActivity extends Activity {private Button button;//private Button readButton;private EditText account;private EditText password;String TAG = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);account = (EditText) findViewById(R.id.account);password = (EditText) findViewById(R.id.passeord);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubSharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();editor.putString("name", account.getText().toString());editor.putString("password", password.getText().toString());editor.commit();finish();}});}}在這個Activity中,通過按鍵的點擊事件通過getSharedPreferences()方法指定一個data的文件,想這個文件里面添加name和password數據。通過查詢data可以看到已經添加進去的數據。
這樣數據就保存成功了。
2、實現對用戶名和密碼的保存
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:stretchColumns="1" ><TableRow ><TextViewandroid:layout_height="wrap_content"android:text="賬號:"/><EditText android:id="@+id/account"android:layout_height="wrap_content"android:hint="請輸入您的賬號"/></TableRow><TableRow ><TextViewandroid:layout_height="wrap_content"android:text="密碼:"/><EditText android:id="@+id/passeord"android:layout_height="wrap_content"android:inputType="textPassword"/></TableRow><TableRow ><CheckBox android:id="@+id/remember_password"android:layout_height="wrap_content"/><TextViewandroid:layout_height="wrap_content"android:text="記住密碼"/></TableRow><TableRow ><Button android:id="@+id/registe"android:layout_height="wrap_content"android:text="注冊"/><Button android:id="@+id/login"android:layout_height="wrap_content"android:text="登陸"/></TableRow></TableLayout> 在布局中添加了一個CheckBox,用戶通過是否選中的方式來選擇是否保存賬號和密碼Activity
package com.example.sharepreferences;import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast;public class LoginActivity extends Activity{private EditText accountEdit;private EditText passwordEdit;private Button login;private SharedPreferences pref;private SharedPreferences.Editor editor;private CheckBox rememberpass;private Button registe;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.login);accountEdit = (EditText) findViewById(R.id.account);passwordEdit = (EditText) findViewById(R.id.passeord);login = (Button) findViewById(R.id.login);rememberpass = (CheckBox) findViewById(R.id.remember_password);registe = (Button) findViewById(R.id.registe);registe.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent(LoginActivity.this, RegisteActivity.class);startActivity(intent);}});pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());boolean isRemember = pref.getBoolean("remember_password", false);if(isRemember){//將賬號和密碼都設置在文本框內String account = pref.getString("account", "");String password = pref.getString("password", "");accountEdit.setText(account);passwordEdit.setText(password);rememberpass.setChecked(true);}login.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString account = accountEdit.getText().toString();String password = passwordEdit.getText().toString();SharedPreferences pre = getSharedPreferences("data", MODE_PRIVATE);if(account.equals(pre.getString("name", ""))&&password.equals(pre.getString("password", ""))){editor = pref.edit();if (rememberpass.isChecked()) {editor.putBoolean("remember_password", true);}else{editor.clear();}editor.commit();Intent intent = new Intent(LoginActivity.this, Mainactivity.class);startActivity(intent);finish();}else {Toast.makeText(LoginActivity.this, "用戶名或密碼不對", Toast.LENGTH_LONG).show();}}});}}在這個Activity中獲取一個pre,查詢本地保存的賬號和密碼,并與用戶輸入的用戶名和密碼進行比較,匹配則登陸成功,否則不成功。總結
以上是生活随笔為你收集整理的android SharedPreferences实现用户的注册和保存账号密码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android ListView与Arr
- 下一篇: 面包车搬家要注意什么?