Android实现记住用户名和密码功能

Android实现记住用户名和密码功能,第1张

概述Android实现记住用户名和密码功能是通过SharedPreference存储来实现的。创建一个复选按钮,通过按钮的否选取来进行事件处理。若按钮选中存储账号和密码的信息。若按钮没有选中,则清空账号和密码的信息。

AndroID 实现记住用户名和密码的功能是通过SharedPreference 存储来实现的。创建一个复选按钮,通过按钮的否选取来进行事件处理。若按钮选中存储账号和密码的信息。若按钮没有选中,则清空账号和密码的信息。

结果演示:

源代码下载地址:

https://github.com/GXS1225/Android――――-.git

分析

(1)判断是否输入了账号和密码

 if(name.trim().equals("")){    Toast.makeText(this,"请您输入用户名!",Toast.LENGTH_SHORT).show();    return;   }   if(pswd.trim().equals("")){    Toast.makeText(this,"请您输入密码!",Toast.LENGTH_SHORT).show();    return;   }

(2)在layout_main.xml定义一个 CheckBox,进行事件处理

//通过boolean CheckBoxLogin = checkBox.isChecked();   //按钮被选中,下次进入时会显示账号和密码          if (CheckBoxLogin)   {     Editor editor = sp.edit();     editor.putString("uname",name);     editor.putString("upswd",pswd);     editor.putBoolean("auto",true);     editor.commit();   }   //按钮被选中,清空账号和密码,下次进入时会显示账号和密码       else      {     Editor editor = sp.edit();    editor.putString("uname",null);    editor.putString("upswd",null);    editor.putBoolean("auto",false);    editor.commit();    }

(3) SharedPreference 的存储实现

//先定义SharedPreferences sp = null; sp = this.getSharedPreferences("userinfo",Context.MODE_PRIVATE);//对uname 和 upswd 的 *** 作 if (sp.getBoolean("checkBoxBoolean",false))   {    uname.setText(sp.getString("uname",null));    upswd.setText(sp.getString("upswd",null));     checkBoxbutton.setChecked(true);   }

(4)跳转到Content.java界面

//Intent跳转Intent intent = new Intent(Welcome.this,Content.class);startActivity(intent);finish();

步骤:

先写一个登陆的界面: layout_main.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"  androID:background="#ADD8E6"> <relativeLayout  androID:ID="@+ID/login_div"  androID:layout_wIDth="fill_parent"  androID:layout_height="221dp"  androID:layout_margin="15dip"  androID:background="@drawable/btn_bg"  androID:padding="15dip" >  <TextVIEw   androID:ID="@+ID/login_user_input"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:layout_alignParenttop="true"   androID:layout_margin="5dp"   androID:text="@string/user"   androID:textSize="16dp"   androID:typeface="sans" />  <EditText   androID:ID="@+ID/user_input"   androID:layout_wIDth="fill_parent"   androID:layout_height="wrap_content"   androID:layout_below="@ID/login_user_input"   androID:background="@androID:drawable/editBox_background"   androID:inputType="text"   androID:singleline="true" />  <TextVIEw   androID:ID="@+ID/login_pass_input"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:layout_below="@ID/user_input"   androID:layout_margin="5dp"   androID:text="@string/pass"   androID:textSize="16dp"   androID:typeface="sans" />  <EditText   androID:ID="@+ID/pass_input"   androID:layout_wIDth="fill_parent"   androID:layout_height="wrap_content"   androID:layout_below="@ID/login_pass_input"   androID:background="@androID:drawable/editBox_background"   androID:inputType="textPassword"   androID:singleline="true" />  <CheckBox   androID:ID="@+ID/checkBoxLogin"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:layout_alignleft="@+ID/pass_input"   androID:layout_alignParentBottom="true"   androID:text="@string/no_user" />  <button   androID:ID="@+ID/new_user"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:layout_alignParentBottom="true"   androID:layout_alignRight="@+ID/pass_input"   androID:layout_marginRight="28dp"   androID:onClick="To_Title"   androID:text="@string/new_user" /> </relativeLayout>  <linearLayout   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:orIEntation="vertical" >   <TextVIEw    androID:layout_wIDth="402dp"    androID:layout_height="51dp"    androID:layout_marginleft="50dp"    androID:background="@drawable/gxs_ziti" />   <button    androID:layout_wIDth="120dp"    androID:layout_height="120dp"    androID:onClick="To_fruist"    androID:background="@drawable/gxs2"     androID:layout_marginleft="80dp"    />  </linearLayout></linearLayout>

Welcome.java

package com.gxs.login;import com.example.login.R;import com.gxs.ListvIEw.*;import androID.os.Bundle;import androID.preference.Preference;import androID.app.Activity;import androID.app.SearchManager.OnCancelListener;import androID.content.Context;import androID.content.Intent;import androID.content.SharedPreferences;import androID.content.SharedPreferences.Editor;import androID.util.Log;import androID.vIEw.Menu;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.CheckBox;import androID.Widget.EditText;import androID.Widget.Toast;public class Welcome extends Activity implements OnClickListener{ private EditText uname = null; private EditText upswd = null; private CheckBox checkBoxbutton = null; private button login = null; SharedPreferences sp = null;  @OverrIDe protected voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.layout_main);  sp = this.getSharedPreferences("userinfo",Context.MODE_PRIVATE);  init(); } public voID init() {  uname = (EditText) findVIEwByID(R.ID.user_input);  upswd = (EditText) findVIEwByID(R.ID.pass_input);  checkBoxbutton = (CheckBox) findVIEwByID(R.ID.checkBoxLogin);  login = (button) findVIEwByID(R.ID.new_user);  if (sp.getBoolean("checkBoxBoolean",null));     checkBoxbutton.setChecked(true);   }  login.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) {  if (v == login){   String name = uname.getText().toString();   String pswd = upswd.getText().toString();   if(name.trim().equals("")){    Toast.makeText(this,Toast.LENGTH_SHORT).show();    return;   }   boolean CheckBoxLogin = checkBoxbutton.isChecked();   if (CheckBoxLogin)   {     Editor editor = sp.edit();     editor.putString("uname",pswd);     editor.putBoolean("checkBoxBoolean",true);     editor.commit();   }   else   {     Editor editor = sp.edit();    editor.putString("uname",null);    editor.putBoolean("checkBoxBoolean",false);    editor.commit();    }   //Intent跳转   Intent intent=new Intent(Welcome.this,Content.class);   startActivity(intent);   finish();  } }}

Content.java

package com.gxs.ListvIEw;import java.util.List;import com.example.login.R;import com.gxs.*;import androID.app.Activity;import androID.os.Bundle;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;public class Content extends Activity{ private ListVIEw ListvIEw_fruits;  @OverrIDe protected voID onCreate(Bundle savedInstanceState) {  // Todo auto-generated method stub  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.content); }}

content.xml

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context=".Welcome"  > <TextVIEw  androID:ID="@+ID/textVIEw1"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:text="内容"  androID:textAppearance="?androID:attr/textAppearanceLarge" /></linearLayout>

更多内容请参考专题:Android密码使用教程

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

总结

以上是内存溢出为你收集整理的Android实现记住用户名和密码功能全部内容,希望文章能够帮你解决Android实现记住用户名和密码功能所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1149202.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存