Android Development Login Case _android

Source: Internet
Author: User
Tags gettext save file

Layout

<?xml version= "1.0"?>-<linearlayout android:paddingtop= "@dimen/activity_vertical_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android :p addingbottom= "@dimen/activity_vertical_margin" android:orientation= "vertical" android:layout_height= "Match_ Parent "android:layout_width=" match_parent "xmlns:tools=" Http://schemas.android.com/tools "xmlns:android=" http:// Schemas.android.com/apk/res/android "> <edittext android:id=" @+id/et_username "android:layout_height=" Wrap_ Content "Android:layout_width=" fill_parent "android:hint=" @string/input_username "/> <edittext android:id=" @+ Id/et_password "android:layout_height=" wrap_content "android:layout_width=" fill_parent "android:hint=" @string Input_password "android:inputtype=" Textpassword "android:layout_marginbottom=" 10DP "android:layout_margintop=" 10DP "/>-<relativelayout android:layout_height=" wrap_content android:layout_width= "fill_parent" > <CHeckbox android:id= "@+id/cb_rem" android:layout_height= "wrap_content" android:layout_width= "Wrap_content" Android: text= "@string/rem_password" android:layout_alignparentleft= "true" android:layout_centervertical= "true"/> < Button android:id= "@+id/bt_login" android:paddingright= "50DP" android:paddingleft= "50DP" android:layout_height= " Wrap_content "android:layout_width=" wrap_content "android:text=" @string/login "android:layout_centervertical=" True ' android:layout_alignparentright= ' true '/> </RelativeLayout> </LinearLayout>

Java Code

Package com.itheima.login;
Import Java.util.Map;
Import Com.itheima.login.util.UserInfoUtil;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.text.TextUtils;
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 Mainactivity extends activity implements onclicklistener{private EditText et_username; private EditText et_p
Assword;
Private CheckBox Cb_rem;
Private Button Bt_login;
Private context Mcontext; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Mcontext = this;
Et_username = (edittext) Findviewbyid (r.id.et_username);
Et_password = (edittext) Findviewbyid (R.id.et_password);
Cb_rem = (CheckBox) Findviewbyid (R.id.cb_rem);
Bt_login = (Button) Findviewbyid (R.id.bt_login); B. Setting the button click event bt_login.sEtonclicklistener (this);
F. Echo user name password?? map<string, string> map = userinfoutil.getuserinfo_android (mcontext);//Get User name password if (map!= null) {String username = m
Ap.get ("username");
String Password = map.get ("password");
Et_username.settext (username);//Set User name Et_password.settext (password); Cb_rem.setchecked (TRUE);/Set checkbox check box selected status} private void Login () {//c. In the OnClick method, get the user name password entered by the user and whether to remember the password String username = et_
Username.gettext (). toString (). Trim ();
String password = Et_password.gettext (). toString (). Trim ();
Boolean isrem = cb_rem.ischecked (); D. Determine if the username password is NULL, NOT NULL request server (omitted, default request succeeded) if (Textutils.isempty (username) | | Textutils.isempty (password)) {Toast.maketext (mcontext, "username password cannot be empty", Toast.length_short). Show (); return;//Request Server,
I'll tell you later. 
E. Determine whether to remember the password, if remember, save the username password locally.????
if (Isrem) {Boolean result = Userinfoutil.saveuserinfo_android (Mcontext,username,password);
Toast.maketext (mcontext, "Username password saved successfully", Toast.length_short). Show (); }else{Toast.maketext (Mcontext, "Save username password failed", Toast.lengtH_short). Show ();
}}else{Toast.maketext (Mcontext, "No need to save", Toast.length_short). Show (); @Override public void OnClick (View v) {switch (V.getid ()) {case r.id.bt_login:login (); break; Default:break}}}}

The code for the new package

Package com.itheima.login.util;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStreamReader;
Import Java.util.HashMap;
Import Java.util.Map;
Import Android.content.Context; public class Userinfoutil {//Save username Password public static Boolean saveuserinfo_android (Context context,string username, String p Assword) {try{String userinfo = username + "# #" + password;//encapsulate username password//Get private directory next file write stream; Name: Private Directory file names mode: mode of operation of files, private
, append, global read, global write FileOutputStream FileOutputStream = context.openfileoutput ("Userinfo.txt", context.mode_private);
Fileoutputstream.write (Userinfo.getbytes ());//write user name password to file Fileoutputstream.close ();
return true;
}catch (Exception e) {e.printstacktrace ();} return false; //Get user name password public static map<string,string> getuserinfo_android (context context) {try{//
Fetching a file read stream of a private directory through the context object FileInputStream FileInputStream = Context.openfileinput ("Userinfo.txt"); BufferedReader BufferedReader= new BufferedReader (new InputStreamReader (FileInputStream));
Reading a line containing the user's password requires parsing String readLine = Bufferedreader.readline ();
string[] split = Readline.split ("# #");
hashmap<string, string> HashMap = new hashmap<string,string> ();
Hashmap.put ("username", split[0]);
Hashmap.put ("Password", split[1]);
Bufferedreader.close ();
Fileinputstream.close ();
return HASHMAP;
}catch (Exception e) {e.printstacktrace ();} return null; //Save Username Password public static Boolean saveuserinfo (context context,string Username, string password) {try{string userinfo =
Username + "# #" + password;//package username password//String path = "/data/data/com.itheima.login/";//Specify Saved path//Get a path to a private directory through the context object
String path = Context.getfilesdir (). GetPath ();
System.out.println ("... ...:" +path) of the "." File File = new file (path, "userinfo.txt");//create file FileOutputStream FileOutputStream = new FileOutputStream (file);
Create file Write Stream Fileoutputstream.write (Userinfo.getbytes ());//write user name password to file Fileoutputstream.close ();
return true; }catch (ExCeption e) {e.printstacktrace ();} return false; //Get user name password public static map<string,string> GetUserInfo (context context) {try{//String path = "/data/data/com.it
Heima.login/";//Specify Saved path//Get a path to a private directory through the context object String Path = Context.getfilesdir (). GetPath ();
System.out.println ("... ...:" +path) of the "."
File File = new file (path, "userinfo.txt");//create file FileInputStream FileInputStream = new FileInputStream (file);
BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (FileInputStream));
Reading a line containing the user's password requires parsing String readLine = Bufferedreader.readline ();
string[] split = Readline.split ("# #");
hashmap<string, string> HashMap = new hashmap<string,string> ();
Hashmap.put ("username", split[0]);
Hashmap.put ("Password", split[1]);
Bufferedreader.close ();
Fileinputstream.close ();
return HASHMAP;
}catch (Exception e) {e.printstacktrace ();} return null; }
}

I have problems and corrections

*alt+enter------Complement Abstract method * *
/* GET global variable * * * How to do fied*/
/* How to format the code * *
/* Click event with global statement * *
/* Many programs use contest, so define objects in the Class! Assignment this, later toast to use direct call * *
/*CTRL+1 Package temporary own taxi class, crate class*/
/* Create method * *
* * Save File/*

1. Save to Private Directory

2. Save the path

3. Create a File object

4. Create a Fileootputstream object, pass file in

* * Private open package, the way to open
* * Save File/*
1. Save to the Private directory/date/date/package name/
2. Save Path (Special)
3. Create a File object (path, filename type plus name)
4. Create a Fileootputstream object, pass file in, in fact, is to create a file write stream
5. To read this piece directly try the Catch output information (what stake)
6.FS invokes the Write method, passing the word stream in. Pass the byte to go in, and can pass one, how to do?
How do I handle the mix with string +?
Add two special characters to enter # # (cannot use regular expression characters). Separated by the method of dividing the string later
7. String invoke self GetByte () method
8. Turn the stream off FS call the Close () method
9. Last return ture tell save success
/*map? */
/*toast*/
1.toast.maketext (Mtext, "Stri ng", Toast. Time). Show
2.mcontext=this, is to create a data
/* When to echo * *
1. The program is loaded back to show
2. Whether to read the file to read
3. Read the same path as the created file
4. Create an input byte stream FIS (F)
4. User name, password is a line, how to read a row
Create a BR object (new IR (F))
5. Create string read bytes BR. RL ()
6. Use of Split string
7. Use hash table for collection
8. Close the Stream

The above is a small set to introduce the Android development landing cases, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.