Android implementation remembers user name and password features

Source: Internet
Author: User

Introduction

First remember that the password function to use the Sharedpreferences class, this class can implement simple storage, mainly for saving the window state

The result diagram is as follows:

Code

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 checkbox = 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);
checkbox = (checkbox) Findviewbyid (R.id.checkboxlogin);
Login = (Button) Findviewbyid (R.id.new_user);
if (Sp.getboolean ("Auto", false))
{
Uname.settext (sp.getstring ("uname", null));
Upswd.settext (sp.getstring ("upswd", null));
Checkbox.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 user name!") ", Toast.length_short). Show ();
Return
}
if (Pswd.trim (). Equals ("")) {
Toast.maketext (This, "Please enter your password!") ", Toast.length_short). Show ();
Return
}
Boolean autologin = checkbox.ischecked ();
if (autologin)
{
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 ();
}
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);

}
}

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:d rawable/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: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/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>

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>

Android implementation remembers user name and password features

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.