源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-09-21 00:48 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android中使用SharedPreferences完成记住账号密码的功能
效果图: [img]http://files.jb51.net/file_images/article/201708/2017082809563914.png[/img] [img]http://files.jb51.net/file_images/article/201708/2017082809563915.png[/img] 记住密码后,再次登录就会出现账号密码,否则没有。 [b]分析:[/b] SharedPreferences可将数据存储到本地的配置文件中 SharedPreferences会记录CheckBox的状态,如果CheckBox被选,则将配置文件中记录的账号密码信息回馈给账号密码控件,否则清空。 [b]SharedPreferences使用方法:[/b] 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>
[b]总结[/b] 以上所述是小编给大家介绍的Android中使用SharedPreferences完成记住账号密码的功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程素材网网站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部