日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android中注册的账号密码储存在,Android中使用SharedPreferences完成记住账号密码的功能...

發布時間:2024/10/12 Android 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android中注册的账号密码储存在,Android中使用SharedPreferences完成记住账号密码的功能... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

效果圖:

記住密碼后,再次登錄就會出現賬號密碼,否則沒有。

分析:

SharedPreferences可將數據存儲到本地的配置文件中

SharedPreferences會記錄CheckBox的狀態,如果CheckBox被選,則將配置文件中記錄的賬號密碼信息回饋給賬號密碼控件,否則清空。

SharedPreferences使用方法:

1、創建名為config的配置文件,并且私有

private SharedPreferences config;

config=getSharedPreferences("config", MODE_PRIVATE);

2、添加編輯器

Editor edit=config.edit();

3、向內存中寫入數據

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);

}

}

//權限要是public,要不然訪問不到

//因為在button控件中設置了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();

//存儲CheckBox的狀態

edit.putBoolean("isCheck", isCheck);

if(isCheck){

edit.putString("username", username).putString("password", password);

}else{

edit.remove("username").remove("password");

}

//提交到本地

edit.commit();

}

}

/記住賬號和密碼/res/layout/activity01.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/et_username"

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>

android:id="@+id/et_password"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

android:id="@+id/cb_choose"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="記住密碼"

/>

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"

/>

總結

以上所述是小編給大家介紹的Android中使用SharedPreferences完成記住賬號密碼的功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!

總結

以上是生活随笔為你收集整理的android中注册的账号密码储存在,Android中使用SharedPreferences完成记住账号密码的功能...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。