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

所属分类: 软件编程 / Android 阅读数: 35
收藏 0 赞 0 分享

效果图:

记住密码后,再次登录就会出现账号密码,否则没有。

分析:

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

更多精彩内容其他人还在看

老生常谈Android HapticFeedback(震动反馈)

下面小编就为大家带来一篇老生常谈Android HapticFeedback(震动反馈)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详谈OnTouchListener与OnGestureListener的区别

下面小编就为大家带来一篇详谈OnTouchListener与OnGestureListener的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿知乎悬浮功能按钮FloatingActionButton效果

前段时间在看属性动画,恰巧这个按钮的效果可以用属性动画实现,下面通过本文给大家分享adroid仿知乎悬浮功能按钮FloatingActionButton效果,需要的朋友参考下吧
收藏 0 赞 0 分享

解决Android V7后自定义Toolbar、ActionBar左侧有空白问题

这篇文章主要介绍的Android V7后自定义Toolbar、ActionBar左侧有空白问题的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android常见控件使用详解

这篇文章主要为大家详细介绍了Android常见控件的使用方法,包括ProgressBar进度条控件、AlertDialog对话框控件等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现简洁的APP更新dialog数字进度条

这篇文章主要为大家详细介绍了Android实现简洁的APP更新dialog数字进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 判断当前语言环境是否是中文环境

本文主要介绍了Android 判断当前语言环境是否是中文环境的方法。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详谈Android中Matrix的set、pre、post的区别

下面小编就为大家带来一篇详谈Android中Matrix的set、pre、post的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现登录界面记住密码的存储

这篇文章主要为大家详细介绍了Android SharedPreferrences实现登录界面记住密码的存储,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 使用SharedPreferrences储存密码登录界面记住密码功能

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入,下面通过实例代码给大家讲解下,需要的朋友参考下吧
收藏 0 赞 0 分享
查看更多