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

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

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences,其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入。所以比较适合我们今天做的这个项目。我们来看一下运行图:

一.布局界面

1.login_top.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="@dimen/activity_horizontal_margin"
 android:background="@drawable/logintop_roundbg">
 <EditText
  android:id="@+id/etName"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:ems="10"
  android:drawablePadding="10dp"
  android:background="@android:drawable/edit_text"
  android:drawableLeft="@drawable/icon_user"
  android:hint="@string/etName">
  <requestFocus></requestFocus>
 </EditText>
 <EditText
  android:id="@+id/etPassword"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@id/etName"
  android:inputType="textPassword"
  android:ems="10"
  android:drawablePadding="10dp"
  android:background="@android:drawable/edit_text"
  android:drawableLeft="@drawable/icon_pass"
  android:hint="@string/etpassword">
  <requestFocus></requestFocus>
 </EditText>
 <CheckBox
  android:id="@+id/cbremenber"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/etPassword"
  android:text="@string/cbpass"/>
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@id/cbremenber">
  <Button
   android:id="@+id/btnlogin"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:background="@drawable/btnselect"
   android:text="@string/btnlogin"
   android:onClick="login"/>
  <Button
   android:id="@+id/btnRegister"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:background="@drawable/btnselect"
   android:text="@string/btnRegister"
   android:layout_marginLeft="10dp"/>
 </LinearLayout>
</RelativeLayout>

2.activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/loginbg"
 tools:context="cn.edu.bzu.logindemo.MainActivity">

 <include layout="@layout/login_top"></include>

 <ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/deer"
  android:layout_alignParentBottom="true"
  android:layout_alignParentRight="true"
  android:layout_alignParentEnd="true" />
</RelativeLayout>

3.activity_welcome.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_welcome"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="cn.edu.bzu.logindemo.WelcomeActivity">

 <TextView
  android:id="@+id/tvwelcome"
  android:text="Welcome you"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="200dp"
  android:textSize="40sp"
   />
</RelativeLayout>

二.MainActivity

 public class MainActivity extends AppCompatActivity {
 private EditText etName;
 private EditText etPassword;
 private SharedPreferences sharedPreferences;
 private CheckBox cbremenber;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initViews();
  sharedPreferences=getSharedPreferences("remenberpassword", Context.MODE_PRIVATE);
  boolean isRemember=sharedPreferences.getBoolean("remenberpassword",false);
  if(isRemember) {
   String name = sharedPreferences.getString("name", "");
   String password = sharedPreferences.getString("password", "");
   etName.setText(name);
   etPassword.setText(password);
   cbremenber.setChecked(true);
  }
 }

 private void initViews() {
  etName=(EditText) findViewById(R.id.etName);
  etPassword=(EditText) findViewById(R.id.etPassword);
  cbremenber=(CheckBox)findViewById(R.id.cbremenber);
 }

 public void login(View view){
  String name=etName.getText().toString();
  String password=etPassword.getText().toString();
  if("admin".equals(name)&&"123456".equals(password)){
   SharedPreferences.Editor editor= sharedPreferences.edit();
   if(cbremenber.isChecked()){
    editor.putBoolean("remenberpassword",true);
    editor.putString("name",name);
    editor.putString("password",password);
   }else {
    editor.clear();
   }
   editor.commit();
   Intent intent=new Intent(this,WelcomeActivity.class);
   startActivity(intent);
   finish();
  }else {
   Toast.makeText(this,"账号或密码有误",Toast.LENGTH_LONG).show();

  }
 }
}

三.WelcomeActivity

 public class WelcomeActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_welcome);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

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

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多