温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中使用SharedPreferences完成记住账号密码的功能

发布时间:2020-10-11 04:15:21 来源:脚本之家 阅读:127 作者:钻石VIP 栏目:移动开发

效果图:

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

<?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完成记住账号密码的功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI