当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现记住用户名和密码功能

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

西安查获传销人员,陆亦子,侍从官之杖

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", false))
   {
    uname.settext(sp.getstring("uname", null));
    upswd.settext(sp.getstring("upswd", 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;
   }
   if(pswd.trim().equals("")){
    toast.maketext(this, 
    "请您输入密码!", toast.length_short).show();
    return;
   }
   boolean checkboxlogin = checkboxbutton.ischecked();
   if (checkboxlogin)
   {
     editor editor = sp.edit();
     editor.putstring("uname", name);
     editor.putstring("upswd", pswd);
     editor.putboolean("checkboxboolean", true);
     editor.commit();
   }
   else
   { 
    editor editor = sp.edit();
    editor.putstring("uname", null);
    editor.putstring("upswd", 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密码使用教程

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网