如何带有自动登陆的登陆界面
如果我们想要实现自动登录的功能就要用到一个知识点“SharedPreferences”,由于SharedPreferences是一个借口,在android平台上是一个轻量级的存储类。所以我们可以用他来实现自动登录的功能。
结果截图:
工程目录:
自动的登录的前提是我们之前已经使用过该软件,并且该APP的本地数据库已经对我们个人使用的账户和密码进行了保存,这样我们就可以使用自动登录了。
1.首先我们先进行页面设计的布局:
(1)新建一个叫做“Login.axml”,"Logo.axml","Welcome.axml"。
布局文件:
Login.axml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/logo_bg" android:orientation="vertical"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageButton android:id="@+id/img_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="@drawable/quit" /> <TextView android:id="@+id/tv_zh" android:layout_width="wrap_content" android:layout_height="35dip" android:layout_marginLeft="12dip" android:layout_marginTop="10dip" android:gravity="bottom" android:text="帐号:" android:textColor="#000000" android:textSize="18sp" /> <EditText android:id="@+id/et_zh" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_below="@id/tv_zh" android:layout_marginLeft="12dip" android:layout_marginRight="10dip" /> <TextView android:id="@+id/tv_mima" android:layout_width="wrap_content" android:layout_height="35dip" android:layout_below="@id/et_zh" android:layout_marginLeft="12dip" android:layout_marginTop="10dip" android:gravity="bottom" android:text="密码:" android:textColor="#000000" android:textSize="18sp" /> <EditText android:id="@+id/et_mima" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_below="@id/tv_mima" android:layout_marginLeft="12dip" android:layout_marginRight="10dip" android:maxLines="200" android:password="true" android:scrollHorizontally="true" /> <CheckBox android:id="@+id/cb_mima" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/et_mima" android:layout_marginLeft="12dip" android:text="记住密码" android:textColor="#000000" /> <CheckBox android:id="@+id/cb_auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/cb_mima" android:layout_marginLeft="12dip" android:text="自动登录" android:textColor="#000000" /> <Button android:id="@+id/btn_login" android:layout_width="80dip" android:layout_height="40dip" android:layout_below="@id/et_mima" android:layout_alignParentRight="true" android:layout_alignTop="@id/cb_auto" android:layout_marginRight="10dip" android:gravity="center" android:text="登录" android:textColor="#000000" android:textSize="18sp" /> </RelativeLayout> </LinearLayout>
Logo.axml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/logo_bg" android:orientation="vertical"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3"> <ProgressBar android:id="@+id/pgBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/pgBar" android:layout_centerHorizontal="true" android:text="正在登录..." android:textColor="#000000" android:textSize="18sp" /> </RelativeLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/btn_back" android:layout_width="70dip" android:layout_height="35dip" android:text="取消" android:textColor="#000000" android:textSize="12sp" /> </LinearLayout> </LinearLayout>
Welcome.axml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:background="@drawable/login_bg" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="登陆成功,进入用户界面" android:textColor="#000000" android:textSize="20sp" /> </LinearLayout>
这样一个完整的布局文件就实现了。
自动登录的代码就像是我们之前写的那种和网站或者是软件的记住密码一样的功能,只是我现在实现的这个没有那个选中是否记住密码的选择,而是直接的默认记住密码。
2.我们把登录的代码写好。
代码部分:
LoginActivity.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; namespace SharedPreferencesStudy { [Activity(Label = "My Activity", MainLauncher = true)] { private EditText userName, password; private CheckBox rem_pw, auto_login; private Button btn_login; private ImageButton btnQuit; private String userNameValue, passwordValue; private ISharedPreferences sp; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.login); //去除标题 ActionBar.Hide(); //获得实例对象 sp = this.GetSharedPreferences("userInfo", 0); userName = (EditText)FindViewById(Resource.Id.et_zh); password = (EditText)FindViewById(Resource.Id.et_mima); rem_pw = (CheckBox)FindViewById(Resource.Id.cb_mima); auto_login = (CheckBox)FindViewById(Resource.Id.cb_auto); btn_login = (Button)FindViewById(Resource.Id.btn_login); btnQuit = (ImageButton)FindViewById(Resource.Id.img_btn); userNameValue = userName.Text; passwordValue = password.Text; //判断记住密码多选框的状态 if (sp.GetBoolean("ISCHECK", false)) { //设置默认是记录密码状态 rem_pw.Checked = true; userNameValue = sp.GetString("USER_NAME", ""); passwordValue = sp.GetString("PASSWORD", ""); //判断自动登陆多选框状态 if (sp.GetBoolean("AUTO_ISCHECK", false)) { //设置默认是自动登录状态 auto_login.Checked = true; //跳转界面 Intent intent = new Intent(this, typeof(LogoActivity)); StartActivity(intent); } } // 登录监听事件 现在默认为用户名为:aaa 密码:123 FindViewById<Button>(Resource.Id.btn_login).Click += (e, s) => { userNameValue = userName.Text.ToString(); passwordValue = password.Text.ToString(); if (userNameValue.Equals("aaa") && passwordValue.Equals("123")) { Toast.MakeText(this, "登录成功", ToastLength.Short).Show(); //登录成功和记住密码框为选中状态才保存用户信息 if (rem_pw.Checked) { //记住用户名、密码、 ISharedPreferencesEditor editor = sp.Edit(); editor.PutString("USER_NAME", userNameValue); editor.PutString("PASSWORD", passwordValue); editor.Commit(); } //跳转界面 Intent intent = new Intent(this, typeof(LogoActivity)); StartActivity(intent); //finish(); } else { Toast.MakeText(this, "用户名或密码错误,请重新登录", ToastLength.Short).Show(); } }; //监听记住密码多选框按钮事件 rem_pw.Click += (o, e) => { if (rem_pw.Checked) { Toast.MakeText(this, "记住密码已选中", ToastLength.Short).Show(); sp.Edit().PutBoolean("ISCHECK", true).Commit(); } else { Toast.MakeText(this, "记住密码没有选中", ToastLength.Short).Show(); sp.Edit().PutBoolean("ISCHECK", false).Commit(); } }; //监听自动登录多选框事件 auto_login.Click += (o, e) => { if (auto_login.Checked) { Toast.MakeText(this, "自动登录已选中", ToastLength.Short).Show(); sp.Edit().PutBoolean("AUTO_ISCHECK", true).Commit(); } else { Toast.MakeText(this, "自动登录没有选中", ToastLength.Short).Show(); sp.Edit().PutBoolean("AUTO_ISCHECK", false).Commit(); } }; } } }
我们接下来写的就是一个登录时一个缓冲的页面
LogoActivity.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; namespace SharedPreferencesStudy { [Activity(Label = "My Activity")] public class LogoActivity : Activity { private ProgressBar progressBar; private Button backButton; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.logo); progressBar = (ProgressBar)FindViewById(Resource.Id.pgBar); backButton = (Button)FindViewById(Resource.Id.btn_back); Intent intent = new Intent(this, typeof(WelcomeActivity)); StartActivity(intent); FindViewById<Button>(Resource.Id.btn_back).Click += (e, s) => { Finish(); }; } } }
经过缓冲后,进入软件。
WelcomeActivity.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; namespace SharedPreferencesStudy { [Activity(Label = "My Activity")] public class WelcomeActivity : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.welcome); } } }
这样一个简单的自动登录的功能就实现了,可能我写的会有些看不明的地方,大家可以自己动手试着就操作理解一下。