Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android中使用SharedPreferences完成记住账号密码的功能大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

效果图:

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,Toast.LENGTH_SHORT).show();
    //存储checkBox的状态
    edit.putBoolean("ischeck",ischeck);
    if(ischeck){
      edit.putString("username",password);
    }else{
      edit.remove("username").remove("password");
    }
    //提交到本地
    edit.commit();
  }
}

/记住账号和密码/res/layout/activity01.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://scheR_621_11845@as.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中使用SharedPreferences完成记住账号密码的功能全部内容,希望文章能够帮你解决Android中使用SharedPreferences完成记住账号密码的功能所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:android