欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年08月28日 09:55:57   作者:鉆石VIP  
這篇文章主要介紹了Android中使用SharedPreferences完成記住賬號(hào)密碼的功能,需要的朋友可以參考下

效果圖:

記住密碼后,再次登錄就會(huì)出現(xiàn)賬號(hào)密碼,否則沒(méi)有。

分析:

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)存中寫(xiě)入數(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,要不然訪問(wèn)不到
  //因?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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android中ListView分頁(yè)加載數(shù)據(jù)功能實(shí)現(xiàn)

    Android中ListView分頁(yè)加載數(shù)據(jù)功能實(shí)現(xiàn)

    本篇文章主要介紹了Android中ListView分頁(yè)加載數(shù)據(jù)功能實(shí)現(xiàn),具有一定的參考價(jià)值,有需要的可以了解一下。
    2016-11-11
  • Kotlin中@JvmOverloads注解作用示例介紹

    Kotlin中@JvmOverloads注解作用示例介紹

    在Kotlin中@JvmOverloads注解的作用就是:在有默認(rèn)參數(shù)值的方法中使用@JvmOverloads注解,則Kotlin就會(huì)暴露多個(gè)重載方法??赡苓€是云里霧里,下面來(lái)詳細(xì)了解
    2022-09-09
  • android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽(tīng)

    android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽(tīng)

    本篇文章主要介紹了android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽(tīng),具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-09-09
  • Android實(shí)現(xiàn)EditText輸入金額

    Android實(shí)現(xiàn)EditText輸入金額

    EditText是Android中一個(gè)非常實(shí)用的控件,有很多InputType,可以來(lái)達(dá)到不同的輸入效果,下面通過(guò)實(shí)例代碼給大家解析android實(shí)現(xiàn)edittext輸入金額,需要的朋友參考下吧
    2016-12-12
  • Android時(shí)間日期拾取器學(xué)習(xí)使用(DatePicker、TimePicker)

    Android時(shí)間日期拾取器學(xué)習(xí)使用(DatePicker、TimePicker)

    這篇文章主要為大家詳細(xì)介紹了Android提供的DatePicker日期拾取器和TimePicker時(shí)間拾取器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android Popwindow彈出框的模板使用示例

    Android Popwindow彈出框的模板使用示例

    這篇文章給大家介紹了Android Popwindow彈出框的模板使用示例,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧
    2017-06-06
  • Android 改變圖標(biāo)原有顏色和搜索框的實(shí)例代碼

    Android 改變圖標(biāo)原有顏色和搜索框的實(shí)例代碼

    讓Android也能有iOS那么方便的圖片色調(diào)轉(zhuǎn)換,就像同一個(gè)圖標(biāo),但是有多個(gè)地方使用,并且顏色不一樣,就可以用這個(gè)方法了。 本文實(shí)現(xiàn)TextView圖片和文字居中,鍵盤(pán)搜索功能,具體實(shí)現(xiàn)代碼大家跟隨腳本之家小編看看吧
    2017-09-09
  • Android 中的危險(xiǎn)權(quán)限詳細(xì)整理

    Android 中的危險(xiǎn)權(quán)限詳細(xì)整理

    這篇文章主要介紹了Android 中的危險(xiǎn)權(quán)限詳細(xì)整理的相關(guān)資料,Android 中有上百種權(quán)限,現(xiàn)在將所有的權(quán)限歸為兩類(lèi),一類(lèi)是普通權(quán)限,一類(lèi)的危險(xiǎn)權(quán)限,危險(xiǎn)權(quán)限則表示那些可能會(huì)觸及到用戶(hù)安全隱私或者對(duì)設(shè)備安全造成影響的權(quán)限,需要的朋友可以參考下
    2017-07-07
  • 詳解Android中BroadCastReceiver組件

    詳解Android中BroadCastReceiver組件

    這篇文章主要為大家詳細(xì)介紹了Android中BroadCastReceiver組件,Broadcast Receiver是Android的五大組件之一,使用頻率也很高,用于異步接收廣播Intent,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Android通過(guò)自定義View實(shí)現(xiàn)隨機(jī)驗(yàn)證碼

    Android通過(guò)自定義View實(shí)現(xiàn)隨機(jī)驗(yàn)證碼

    這篇文章主要介紹了Android通過(guò)自定義View實(shí)現(xiàn)隨機(jī)驗(yàn)證碼的相關(guān)資料,需要的朋友可以參考下
    2016-03-03

最新評(píng)論