In this example, you can use the sharedpreferences storage and custom file storage methods provided by the Android platform.
1. Follow the following configuration and the method described in step-by-step instance 1 to create the base of the entire project:
Project name: examplethree
Platform: android2.0;
Application name: examplethree
Package name: COM. Example
Activity: mainactivity
Resource file: Main. xml
Main. XML has two textviews:
ID: @ + ID/textview01
Text: Username:
ID: @ + ID/textview02
Text: password:
Main. XML has two editviews:
ID: @ + ID/editusername
Text: blank
Layout width: fill_parent
ID: @ + ID/editpassword
Text: blank
Password: True
Layout width: fill_parent
Main. XML has two buttons:
ID: @ + ID/savebypref
Text: save by preference
ID: @ + ID/savetofile
Text: Save to self-defined file
2. Add the following code to mainactivity:
Definition of constants:
Public static final string section_name = "examplethree ";
Public static final string username = "username ";
Public static final string Password = "password ";
Private Static final string file_name = "userinfo. pwd ";
Module functions:
Private void load_values_from_file (){
String Path = This. getfilesdir (). getabsolutepath () + file. Separator + file_name;
File file = new file (PATH );
If (file. exists ()){
Show_file_values ();
}
}
Private void show_file_values (){
Fileinputstream FCM = NULL;
Datainputstream Dis = NULL;
Try {
FS = openfileinput (file_name );
Dis = new datainputstream (FCM );
String strusername = dis. readutf ();
String strpassword = dis. readutf ();
Edittext evusername = (edittext) findviewbyid (R. Id. editusername );
Edittext evpassword = (edittext) findviewbyid (R. Id. editpassword );
Evusername. settext (strusername );
Evpassword. settext (strpassword );
} Catch (ioexception IOE ){
IOE. printstacktrace ();
} Finally {
If (DIS! = NULL ){
Try {
Dis. Close ();
} Catch (ioexception IOE ){
}
}
}
}
Private void attach_button_events (){
Button buttonsavebypref = (button) findviewbyid (R. Id. savebypref );
Buttonsavebypref. setonclicklistener (savebypref_listener );
Button buttonsavetofile = (button) findviewbyid (R. Id. savetofile );
Buttonsavetofile. setonclicklistener (savetofile_listener );
}
Private button. onclicklistener savebypref_listener = new button. onclicklistener (){
Public void onclick (view v ){
Edittext evusername = (edittext) findviewbyid (R. Id. editusername );
Edittext evpassword = (edittext) findviewbyid (R. Id. editpassword );
Sharedpreferences settings = getsharedpreferences (section_name, 0 );
Settings. Edit (). putstring (username, evusername. gettext (). tostring (). putstring (password,
Evpassword. gettext (). tostring (). Commit ();
}
};
Private button. onclicklistener savetofile_listener = new button. onclicklistener (){
Public void onclick (view v ){
Edittext evusername = (edittext) findviewbyid (R. Id. editusername );
Edittext evpassword = (edittext) findviewbyid (R. Id. editpassword );
String strusername = evusername. gettext (). tostring ();
String strpassword = evpassword. gettext (). tostring ();
Dataoutputstream dos = NULL;
Try {
DOS = new dataoutputstream (openfileoutput (file_name, mode_private ));
Dos. writeutf (strusername );
Dos. writeutf (strpassword );
} Catch (ioexception IOE ){
IOE. printstacktrace ();
} Finally {
If (dos! = NULL ){
Try {
Dos. Close ();
} Catch (ioexception IOE ){
}
}
}
}
};
Add the following code at the end of the oncreate () overload function:
Load_values_from_file ();
Attach_button_events ();
In this section, pay special attention to the file path acquisition method;
3. After the above Code is pasted, the next part will be the test part. Note that username and password cannot be empty (for simplicity, this program does not detect empty strings ),
After the file is generated, you can open the ddms view in WINDOW> open perspective> ddms. In this view, select the Tab View of file explorer in the right bar and open the following address:
Data/data/COM. under example, you can view the shared_prefs and files directories respectively, and you will find examplethree. XML and userinfos. PWD corresponds to the files generated by the savebyprefs and save by self-defined file functions in this example respectively.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/jackxinxu2100/archive/2010/01/26/5258924.aspx