Android中使用SharedPreferences完成記住賬號(hào)密碼的功能
效果圖:


記住密碼后,再次登錄就會(huì)出現(xiàn)賬號(hào)密碼,否則沒有。
分析:
SharedPreferences可將數(shù)據(jù)存儲(chǔ)到本地的配置文件中
SharedPreferences會(huì)記錄CheckBox的狀態(tài),如果CheckBox被選,則將配置文件中記錄的賬號(hào)密碼信息回饋給賬號(hào)密碼控件,否則清空。
SharedPreferences使用方法:
1、創(chuàng)建名為config的配置文件,并且私有
private SharedPreferences config;
config=getSharedPreferences("config", MODE_PRIVATE);
2、添加編輯器
Editor edit=config.edit();
3、向內(nèi)存中寫入數(shù)據(jù)
String username=et_username.getText().toString();
String password=et_password.getText().toString();
edit.putString("username", username).putString("password", password);
4、提交到本地
edit.commit();
代碼:
fry.Activity01
package fry;
import com.example.rememberUserAndPassword.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
public class Activity01 extends Activity{
private Button btn_login;
private TextView et_username;
private TextView et_password;
private CheckBox cb_choose;
private SharedPreferences config;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01);
config=getSharedPreferences("config", MODE_PRIVATE);
btn_login=(Button) findViewById(R.id.btn_login);
et_username=(TextView) findViewById(R.id.et_username);
et_password=(TextView) findViewById(R.id.et_password);
cb_choose=(CheckBox) findViewById(R.id.cb_choose);
//是否記住了密碼,初始化為false
boolean isCheck=config.getBoolean("isCheck", false);
//Toast.makeText(this, isCheck+" ", Toast.LENGTH_SHORT).show();
if(isCheck){
et_username.setText(config.getString("username", ""));
et_password.setText(config.getString("password", ""));
cb_choose.setChecked(isCheck);
}
}
//權(quán)限要是public,要不然訪問不到
//因?yàn)樵赽utton控件中設(shè)置了android:onClick="onClick"
public void onClick(View view){
Toast.makeText(this, "登錄成功", Toast.LENGTH_SHORT).show();
Editor edit=config.edit();
String username=et_username.getText().toString();
String password=et_password.getText().toString();
boolean isCheck=cb_choose.isChecked();
//Toast.makeText(this, isCheck+" ", Toast.LENGTH_SHORT).show();
//存儲(chǔ)CheckBox的狀態(tài)
edit.putBoolean("isCheck", isCheck);
if(isCheck){
edit.putString("username", username).putString("password", password);
}else{
edit.remove("username").remove("password");
}
//提交到本地
edit.commit();
}
}
/記住賬號(hào)和密碼/res/layout/activity01.xml
<?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" >
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="@+id/cb_choose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="記住密碼"
/>
</LinearLayout>
<!-- android:onClick="onClick" 點(diǎn)擊時(shí)去class中調(diào)用onClick方法,權(quán)限要為public -->
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄"
android:layout_gravity="center_horizontal"
android:onClick="onClick"
/>
</LinearLayout>
總結(jié)
以上所述是小編給大家介紹的Android中使用SharedPreferences完成記住賬號(hào)密碼的功能,希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android SharedPreferences存取操作以及封裝詳解
- Android 文件存儲(chǔ)與SharedPreferences存儲(chǔ)方式詳解用法
- Android 使用 SharedPreferences 保存少量數(shù)據(jù)的實(shí)現(xiàn)代碼
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄
- Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能
- Android開發(fā)中4個(gè)常用的工具類【Toast、SharedPreferences、網(wǎng)絡(luò)及屏幕操作】
- Android數(shù)據(jù)共享 sharedPreferences 的使用方法
- Android中SharedPreferences簡單使用實(shí)例
- Android SharedPreferences四種操作模式使用詳解
- 使用SharedPreferences在Android存儲(chǔ)對(duì)象詳細(xì)代碼
相關(guān)文章
Android MediaPlayer實(shí)現(xiàn)音樂播放器實(shí)例代碼
這篇文章主要介紹了Android MediaPlayer實(shí)現(xiàn)音樂播放器實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android為按鈕控件綁定事件的五種實(shí)現(xiàn)方式
本篇文章主要是介紹了Android為按鈕控件綁定事件的五種實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
Android實(shí)現(xiàn)多個(gè)連續(xù)帶數(shù)字圓圈效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多個(gè)連續(xù)帶數(shù)字圓圈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
超簡單實(shí)現(xiàn)Android自定義Toast示例(附源碼)
本篇文章主要介紹了超簡單實(shí)現(xiàn)Android自定義Toast示例(附源碼),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02
Android實(shí)現(xiàn)可以展開的TextView
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可以展開的TextView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
android studio3.3.1代碼提示忽略大小寫的設(shè)置
這篇文章主要介紹了android studio3.3.1代碼提示忽略大小寫的設(shè)置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03

