Android implementation remembers user name and password features _android

Source: Internet
Author: User
Tags gettext

The Android implementation remembers that the user name and password functions are implemented through sharedpreference storage. Create a check button to handle event handling through a button's no selection. If the button selects the information that stores the account number and password. If the button is not selected, empty the account and password information.

Results Demo:

Source code Download Address:

Https://github.com/GXS1225/Android ————-. git

Analysis

(1) Decide whether to enter the account number and password

  if (Name.trim (). Equals ("")) {
        Toast.maketext (this,) Please enter your username! ", Toast.length_short). Show ();
        return;
      }
      if (Pswd.trim (). Equals ("")) {
        Toast.maketext (this,) Please enter your password! ", Toast.length_short). Show ();
        return;
      }

(2) Define a CheckBox in Layout_main.xml for event handling

Via
Boolean checkboxlogin = checkbox.ischecked ();
      button is selected, the next entry will show the account number and password             
      if (checkboxlogin)
      {
          Editor Editor = Sp.edit ();
          Editor.putstring ("uname", name);
          Editor.putstring ("upswd", pswd);
          Editor.putboolean ("Auto", true);
          Editor.commit ();
      }
      button is selected, empty the account and password, the next entry will show the account number and password       
      else      
      { 
        Editor Editor = Sp.edit ();
        Editor.putstring ("uname", null);
        Editor.putstring ("upswd", null);
        Editor.putboolean ("Auto", false);
        Editor.commit ();
        }

(3) Storage implementation of Sharedpreference

Define
Sharedpreferences sp = null first; 

SP = this.getsharedpreferences ("userinfo", context.mode_private);
Operations on uname and UPSWD
 if (Sp.getboolean ("Checkboxboolean", false))
      {uname.settext (
        sp.getstring ("uname"), NULL));
        Upswd.settext (sp.getstring ("upswd", null)); 
        Checkboxbutton.setchecked (true);

      

(4) Jump to Content.java interface

Intent Jump
Intent Intent = new Intent (welcome.this,content.class);
StartActivity (intent);
Finish ();

Steps:

First to write a landing interface: 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_pare NT "android:layout_height=" 221DP "android:layout_margin=" 15dip "android:background=" @drawable/btn_bg "Andr oid:padding= "15dip" > <textview android:id= "@+id/login_user_input" android:layout_width= "Wrap_conte" NT "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:d rawable/editbox_background "android:inputtype=" text android:s Ingleline= "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:d rawable/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/pas" S_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_alignparentbott Om= "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" > <textvie W android:layout_width= "402DP" android:layout_height= "51DP" android:layout_marginleft= "50DP" a ndroid:background= "@drawable/gxs_ziti"/> <button android:layout_width= "120DP" android:layout_he ight= "120DP" android:onclick= "to_fruist" android:background= "@drawable/gxs2" Android:layout_marginle

ft= "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 = 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,) Please enter your username!
        ", Toast.length_short). Show ();
      Return } if (Pswd.trim (). Equals ("")) {Toast.maketext (this,) Please enter your password! ", 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 Jump 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=" content "
    android:textappearance="? android:attr/ Textappearancelarge "/>

</LinearLayout>

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.