Android中SharedPreference使用實(shí)例講解
SharedPreference方面的內(nèi)容還算是比較簡(jiǎn)單易懂的,在此還是主要貼上效果與代碼,最后也是附上源碼。
首先是輸入賬號(hào)admin,密碼123,選擇記住密碼登陸。
登陸后就直接跳轉(zhuǎn)頁(yè)面。

隨后再次打開(kāi)app可以發(fā)現(xiàn)已經(jīng)記住了密碼。
如果輸錯(cuò)了密碼,或者在登陸的時(shí)候沒(méi)有選擇記住密碼,那么會(huì)將SharedPreference的文件清空,再次登陸后又會(huì)是空的EditText。

MainActivity:
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
//使用SharedPreferences進(jìn)行讀取
private SharedPreferences pref;
//使用SharedPreferences.Editor進(jìn)行存儲(chǔ)
private SharedPreferences.Editor editor;
private Button button;
private CheckBox checkbox;
private EditText accountEdit;
private EditText passwordEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//取消標(biāo)題
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//制定SharedPreference的文件名為data
pref= getSharedPreferences("data",MODE_PRIVATE);
editor=pref.edit();
accountEdit=(EditText)findViewById(R.id.account);
passwordEdit=(EditText)findViewById(R.id.password);
checkbox=(CheckBox)findViewById(R.id.remember_password);
button=(Button)findViewById(R.id.button);
//查看app中是否已經(jīng)存儲(chǔ)過(guò)賬號(hào)密碼,有的話就直接顯示出來(lái)
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);
checkbox.setChecked(true);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String account=accountEdit.getText().toString();
String password=passwordEdit.getText().toString();
//此處設(shè)置的賬號(hào)為“admin”,密碼為“123”
if(account.equals("admin")&&password.equals("123")){
//如果記住密碼被選中,那么就將賬號(hào)密碼存儲(chǔ)起來(lái)
//如果沒(méi)有被選中,那么將存儲(chǔ)的賬號(hào)密碼清空
if(checkbox.isChecked()){
editor.putBoolean("remember_password",true);
editor.putString("account",account);
editor.putString("password",password);
}else{
editor.clear();
}
//最后千萬(wàn)不要忘記使用commit將添加的數(shù)據(jù)提交
editor.commit();
//簡(jiǎn)單的跳轉(zhuǎn)界面
Intent intent=new Intent(MainActivity.this,NextActivity.class);
startActivity(intent);
finish();
}else{
//如果賬號(hào)密碼錯(cuò)誤,就跳出toast提示,并且清空存儲(chǔ)的賬號(hào)密碼
Toast.makeText(MainActivity.this,"賬號(hào)或密碼錯(cuò)誤!",Toast.LENGTH_LONG).show();
editor.clear();
editor.commit();
}
}
});
}
}
activity_main:
<TableLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:stretchColumns="1" tools:context=".MainActivity"> <TableRow> <TextView android:textSize="20sp" android:text="賬號(hào):" android:textColor="#000" android:layout_height="wrap_content"/> <EditText android:id="@+id/account" android:hint="請(qǐng)輸入你的賬號(hào)" /> </TableRow> <TableRow> <TextView android:textSize="20sp" android:text="密碼:" android:textColor="#000" android:layout_height="wrap_content"/> <EditText android:id="@+id/password" android:hint="請(qǐng)輸入你的密碼" android:password="true" /> </TableRow> <TableRow> <CheckBox android:id="@+id/remember_password" android:layout_height="wrap_content" android:layout_gravity="center"/> <TextView android:textSize="20sp" android:text="記住密碼" android:layout_height="wrap_content"/> </TableRow> <TableRow> <Button android:id="@+id/button" android:layout_span="2" android:text="確定" android:textSize="25sp"/> </TableRow> </TableLayout>
NextActivity:
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
public class NextActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_next);
}
}
activity_next:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登陸成功!" android:textSize="30sp" android:layout_gravity="center_horizontal" /> </LinearLayout>
希望本文所述對(duì)大家學(xué)習(xí)Android程序設(shè)計(jì)有所幫助。
- Android之PreferenceActivity應(yīng)用詳解(2)
- Android之PreferenceActivity應(yīng)用詳解
- Android 清除SharedPreferences 產(chǎn)生的數(shù)據(jù)(實(shí)例代碼)
- android中使用SharedPreferences進(jìn)行數(shù)據(jù)存儲(chǔ)的操作方法
- Android中刪除Preference詳解
- Android SharedPreferences的使用分析
- Android設(shè)置PreferenceCategory背景顏色的方法
- Android編程之ListPreference用法實(shí)例分析
- android開(kāi)發(fā)基礎(chǔ)教程—SharedPreferences讀寫(xiě)
- Android學(xué)習(xí)筆記之Shared Preference
- Android PreferenceActivity與PreferenceFragment詳解及簡(jiǎn)單實(shí)例
相關(guān)文章
Android?RecyclerChart其它圖表繪制示例詳解
這篇文章主要為大家介紹了Android?RecyclerChart其它圖表繪制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android實(shí)現(xiàn)密碼明密文切換(小眼睛)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)密碼明密文切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Flutter最小刷新范圍探索ValueListenableBuilder使用詳解
這篇文章主要為大家介紹了Flutter最小刷新范圍探索ValueListenableBuilder使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
解決EditText不顯示光標(biāo)的三種方法(總結(jié))
下面小編就為大家?guī)?lái)一篇解決EditText不顯示光標(biāo)的三種方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
XListView實(shí)現(xiàn)多條目網(wǎng)絡(luò)數(shù)據(jù)刷新加載 網(wǎng)絡(luò)加載圖片
這篇文章主要為大家詳細(xì)介紹了XListView實(shí)現(xiàn)多條目網(wǎng)絡(luò)數(shù)據(jù)刷新加載,網(wǎng)絡(luò)加載圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android Usb設(shè)備的監(jiān)聽(tīng)(Dev)外設(shè)端口的判定以及耳機(jī)的插拔
今天小編就為大家分享一篇關(guān)于Android Usb設(shè)備的監(jiān)聽(tīng)(Dev)外設(shè)端口的判定以及耳機(jī)的插拔,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Android 日常開(kāi)發(fā)總結(jié)的60條技術(shù)經(jīng)驗(yàn)
這篇文章主要介紹了Android日常開(kāi)發(fā)總結(jié)的技術(shù)經(jīng)驗(yàn)60條,需要的朋友可以參考下2016-03-03
詳解Android項(xiàng)目多服務(wù)端接口適配(超簡(jiǎn)單)
這篇文章主要介紹了Android項(xiàng)目多服務(wù)端接口適配(超簡(jiǎn)單),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

