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

效果圖:

在富川等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需開(kāi)發(fā)網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),成都全網(wǎng)營(yíng)銷,成都外貿(mào)網(wǎng)站建設(shè),富川網(wǎng)站建設(shè)費(fèi)用合理。

Android中使用SharedPreferences完成記住賬號(hào)密碼的功能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)存中寫入數(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ì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

網(wǎng)站欄目:Android中使用SharedPreferences完成記住賬號(hào)密碼的功能
文章路徑:http://muchs.cn/article32/gheopc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、全網(wǎng)營(yíng)銷推廣虛擬主機(jī)、營(yíng)銷型網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)