Android login interface is implemented through file storage

Source: Internet
Author: User

 

Layout code:

<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: orientation = "vertical">


<! -- User Name layout -->


<LinearLayout

Android: layout_width = "match_parent"

Android: layout_height = "wrap_content">


<TextView

Android: id = "@ + id/view_name"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "@ string/text_name"/>


<EditText

Android: id = "@ + id/edit_name"

Android: layout_width = "0dp"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"

Android: EMS = "10"

Android: inputType = "textPersonName">


<RequestFocus/>

</EditText>

</LinearLayout>

<! -- Password layout -->


<LinearLayout

Android: layout_width = "match_parent"

Android: layout_height = "wrap_content">


<TextView

Android: id = "@ + id/view_pass"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "@ string/text_pass"/>


<EditText

Android: id = "@ + id/edit_pass"

Android: layout_width = "0dp"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"

Android: EMS = "10"

Android: inputType = "textPassword">


<RequestFocus/>

</EditText>

</LinearLayout>


<LinearLayout

Android: layout_width = "match_parent"

Android: layout_height = "wrap_content">


<Button

Android: id = "@ + id/btn_login"

Android: layout_marginLeft = "0dp"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "@ string/text_login"/>


<CheckBox

Android: id = "@ + id/cbx_rember"

Android: layout_marginLeft = "100dp"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "@ string/text_rember"/>

</LinearLayout>


</LinearLayout>

Code in Value: <? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>


<String name = "app_name"> lession02-file </string>

<String name = "action_settings"> Settings </string>

<String name = "hello_world"> Hello world! </String>

<String name = "text_name"> User name: </string>

<String name = "text_pass"> password: </string>

<String name = "text_login"> login </string>

<String name = "text_rember"> Remember password </string>


</Resources>

:

 


Code in logActivi:

Package com. example. file;


Import java. io. IOException;

Import java. util. Map;


Import www.csdn.net. service. FileService;


Import android. app. Activity;

Import android. OS. Bundle;

Import android. text. TextUtils;

Import android. view. Menu;

Import android. view. View;

Import android. widget. Button;

Import android. widget. CheckBox;

Import android. widget. EditText;

Import android. widget. Toast;

 


Public class LoginActivity extends Activity {


// Declare the user name and password component

Public EditText edit_name, edit_pass;

// Declare the login Button Object

Public Button btn_login;

// Declare the CheckBox Component Object

Public CheckBox box_remember;


// Create a Business Object

Public FileService fileService;


@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

// Set the display view

SetContentView (R. layout. activity_login );


// Instantiate the Business Object

FileService = new FileService (this );


// Obtain the Component Object Based on the id.

Edit_name = (EditText) findViewById (R. id. edit_name );

Edit_pass = (EditText) findViewById (R. id. edit_pass );

Btn_login = (Button) findViewById (R. id. btn_login );

Box_remember = (CheckBox) findViewById (R. id. cbx_rember );


// Register an event for the button

Btn_login.setOnClickListener (new MyOnClickListener ());

 

// Echo data

Map <String, String> map = null;

Try {

Map = fileService. readFile ("private.txt ");

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

If (map! = Null ){

Edit_name.setText (map. get ("name "));

Edit_pass.setText (map. get ("pass "));

}


}


@ Override

Public boolean onCreateOptionsMenu (Menu menu ){

// Inflate the menu; this adds items to the action bar if it is present.

GetMenuInflater (). inflate (R. menu. login, menu );

Return true;

}


// Internal class

Class MyOnClickListener implements View. OnClickListener {

@ Override

Public void onClick (View v ){

Int id = v. getId ();


// Determine whether the selected component is a button

If (id = btn_login.getId ()){


// Obtain the user name and password

String name = edit_name.getText (). toString ();

String pass = edit_pass.getText (). toString ();


// Determine whether the user name and password are empty

If (TextUtils. isEmpty (name) | TextUtils. isEmpty (pass )){

Toast. makeText (LoginActivity. this, "the user name or password cannot be blank ",

Toast. LENGTH_LONG). show ();

Return;

} Else {


// If you remember to check the password

If (box_remember.isChecked ()){

// Save

// Call the Business Method of the Business Object

Try {

LoginActivity. this. fileService. saveToRom (name, pass, "private.txt ");

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

Toast. makeText (LoginActivity. this, "the user name and password need to be saved ",

Toast. LENGTH_LONG). show ();


} Else {

// Do not save

Toast. makeText (LoginActivity. this, "the user name and password do not need to be saved ",

Toast. LENGTH_LONG). show ();

}


}


}


}


}


}

Service;

Package www.csdn.net. service;


Import java. io. FileInputStream;

Import java. io. FileNotFoundException;

Import java. io. FileOutputStream;

Import java. io. IOException;

Import java. util. HashMap;

Import java. util. Map;


Import www.csdn.net. tools. StreamTools;


Import android. content. Context;


Public class FileService {

// Context object

Public Context context;


Public FileService (Context context ){


This. context = context;

}


/**

* User name and password storage operations on the phone memory

* @ Param name

* @ Param pass

* @ Param fileName

* @ Return

* @ Throws IOException

*/

Public boolean saveToRom (String name, String pass, String fileName) throws IOException {

Try {

// Obtain the output stream object of a file through the openFileOutput () method

FileOutputStream fos = context. openFileOutput (fileName, Context. MODE_PRIVATE );

// Concatenate the user name and password

String result = name + ":" + pass;

Fos. write (result. getBytes ());

Fos. flush ();

Fos. close ();

} Catch (FileNotFoundException e ){


E. printStackTrace ();


Return false;

}

Return true;

}

// Read data

Public Map <String, String> readFile (String fileName) throws IOException {

Map <String, String> map = null; // new HashMap <String, String> ();

Try {

FileInputStream FCM = context. openFileInput (fileName );

String value = StreamTools. getValue (FCM );

String values [] = value. split (":");


If (values. length> 0 ){

Map = new HashMap <String, String> ();

Map. put ("name", values [0]);

Map. put ("pass", values [1]);

}

} Catch (FileNotFoundException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

Return map;

}

}

A tool called:

Package www.csdn.net. tools;


Import java. io. ByteArrayOutputStream;

Import java. io. FileInputStream;

Import java. io. IOException;


Public class StreamTools {

Public static String getValue (FileInputStream ISI) throws IOException {

// Byte output stream object

ByteArrayOutputStream stream = new ByteArrayOutputStream ();

Byte [] buffer = new byte [1024];

Int length = 1;

While (length = FS. read (buffer ))! =-1 ){

Stream. write (buffer, 0, length );

}

Stream. flush ();

Stream. close ();

String value = stream. toString ();

Return value;

}

}


 

 

 

In the work Window, find File Explorer and display it to the console:

Data-com.example-files-private.txt

Displayed Number of bureaus: aaa 123

 

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.