国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中使用SharedPreferences完成記住賬號密碼的功能

2019-12-12 02:10:04
字體:
來源:轉載
供稿:網友

效果圖:

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

分析:

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

<?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" 點擊時去class中調用onClick方法,權限要為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>

總結

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德阳市| 齐河县| 田东县| 加查县| 普兰县| 子洲县| 临漳县| 横山县| 龙里县| 务川| 中西区| 万安县| 崇仁县| 福安市| 民和| 平潭县| 宁安市| 内黄县| 西昌市| 邯郸市| 漳浦县| 南康市| 玉溪市| 承德市| 彩票| 兴安盟| 江孜县| 游戏| 万州区| 西乌珠穆沁旗| 湖州市| 金坛市| 合阳县| 水富县| 蒲江县| 新巴尔虎左旗| 大同市| 梁平县| 赣州市| 景洪市| 广东省|