你的位置:首页 > 软件开发 > 操作系统 > 利用SharedPreferences完成记住账号密码的功能

利用SharedPreferences完成记住账号密码的功能

发布时间:2017-08-27 10:00:08
利用SharedPreferences完成记住账号密码的功能效果图:记住密码后,再次登录就会出现账号密码,否则没有。 分析:SharedPreferences可将数据存储到本地的配置文件中SharedPreferences会记录CheckBox的状态,如果CheckBox被选 ...

利用SharedPreferences完成记住账号密码的功能

利用SharedPreferences完成记住账号密码的功能

效果图:

利用SharedPreferences完成记住账号密码的功能利用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

利用SharedPreferences完成记住账号密码的功能利用SharedPreferences完成记住账号密码的功能
 1 package fry; 2  3 import com.example.rememberUserAndPassword.R; 4  5 import android.app.Activity; 6 import android.content.SharedPreferences; 7 import android.content.SharedPreferences.Editor; 8 import android.os.Bundle; 9 import android.view.View;10 import android.widget.Button;11 import android.widget.CheckBox;12 import android.widget.TextView;13 import android.widget.Toast;14 15 public class Activity01 extends Activity{16  private Button btn_login;17  private TextView et_username;18  private TextView et_password;19  private CheckBox cb_choose;20  private SharedPreferences config;21  22  23  @Override24  protected void onCreate(Bundle savedInstanceState) {25   // TODO Auto-generated method stub26   super.onCreate(savedInstanceState);27   setContentView(R.layout.activity01);28   config=getSharedPreferences("config", MODE_PRIVATE);29   btn_login=(Button) findViewById(R.id.btn_login);30   et_username=(TextView) findViewById(R.id.et_username);31   et_password=(TextView) findViewById(R.id.et_password);32   cb_choose=(CheckBox) findViewById(R.id.cb_choose);33   34   //是否记住了密码,初始化为false35   boolean isCheck=config.getBoolean("isCheck", false);36   //Toast.makeText(this, isCheck+" ", Toast.LENGTH_SHORT).show();37   if(isCheck){38    et_username.setText(config.getString("username", ""));39    et_password.setText(config.getString("password", ""));40    cb_choose.setChecked(isCheck);41   }42   43  }44  //权限要是public,要不然访问不到45  //因为在button控件中设置了android:onClick="onClick"46  public void onClick(View view){47   Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();48   Editor edit=config.edit();49   String username=et_username.getText().toString();50   String password=et_password.getText().toString();51   boolean isCheck=cb_choose.isChecked();52   //Toast.makeText(this, isCheck+" ", Toast.LENGTH_SHORT).show();53   //存储CheckBox的状态54   edit.putBoolean("isCheck", isCheck);55   if(isCheck){56    edit.putString("username", username).putString("password", password);57   }else{58    edit.remove("username").remove("password");59   }60   //提交到本地61   edit.commit();62  }63 }
代码逻辑部分

/记住账号和密码/res/layout/activity01.利用SharedPreferences完成记住账号密码的功能利用SharedPreferences完成记住账号密码的功能

 1 <??> 2 <LinearLayout ="http://schemas.android.com/apk/res/android" 3  android:layout_width="match_parent" 4  android:layout_height="match_parent" 5  android:orientation="vertical" > 6  7  <EditText  8   android:id="@+id/et_username" 9   android:layout_width="match_parent"10   android:layout_height="wrap_content"11   />12 13  <EditText14   android:id="@+id/et_password"15   android:layout_width="match_parent"16   android:layout_height="wrap_content"17   android:ems="10" >18 19   <requestFocus />20  </EditText>21 22  <LinearLayout 23   android:layout_width="wrap_content"24   android:layout_height="wrap_content"25   >26   <CheckBox 27     android:id="@+id/cb_choose"28    android:layout_width="wrap_content"29    android:layout_height="wrap_content"30    />31   <TextView 32    android:layout_width="wrap_content"33    android:layout_height="wrap_content"34    android:text="记住密码"35    />36  37  </LinearLayout>38  <!-- android:onClick="onClick" 点击时去class中调用onClick方法,权限要为public -->39  <Button40   android:id="@+id/btn_login"41   android:layout_width="wrap_content"42   android:layout_height="wrap_content"43   android:text="登录"44   android:layout_gravity="center_horizontal"45   android:onClick="onClick"46   />47 </LinearLayout>
界面设计部分

 

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:利用SharedPreferences完成记住账号密码的功能

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录