广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现登录界面记住密码的存储
  • 470
分享到

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

界面存储Android 2022-06-06 01:06:55 470人浏览 薄情痞子
摘要

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入。所以比较

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);
 }
}
您可能感兴趣的文章:Android SharedPreferences实现记住密码和自动登录Android Walker登录记住密码页面功能实现Android开发笔记sqlite优化记住密码功能Android实现用户登录记住密码功能Android sharedPreferences实现记住密码功能Android 使用SharedPreferrences储存密码登录界面记住密码功能Android SharedPreferences实现记住密码和自动登录界面Android实现带有记住密码功能的登陆界面Android通过"记住密码"功能学习数据存储类SharedPreferences详解及实例Android实现记住密码功能


--结束END--

本文标题: Android实现登录界面记住密码的存储

本文链接: https://www.lsjlt.com/news/21836.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作