This article describes the Android development of the completion of the landing interface data saving echo operation. Share to everyone for your reference, specific as follows:
Loginactivity.java:
Package com.example.login;
Import Java.util.Map;
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;
Import Com.example.login.service.FileService;
public class Loginactivity extends activity {public EditText edit_name,edit_pass;
Public Button Btn_login;
Public CheckBox Box_remeber;
Public Fileservice Fileservice;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_login);
Fileservice=new Fileservice (this);
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_remeber= (CheckBox) Findviewbyid (R.id.cbx_remember); Btn_login.setonclickliStener (New Myonclicklistener ());
Map<string, string> map=fileservice.readfile ("Private.txt");
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 AC
tion Bar if it is present.
Getmenuinflater (). Inflate (R.menu.login, menu);
return true; Class Myonclicklistener implements view.onclicklistener{@Override public void OnClick (View v) {int
Id=v.getid ();
Switch (ID) {case r.id.btn_login:string name=edit_name.gettext (). toString ();
String Pass=edit_pass.gettext (). toString ();
if (textutils.isempty (name)) {Toast.maketext (loginactivity.this, "User name cannot be empty", Toast.length_short). Show ();
Return }else if (Textutils.isempty pass) {toast.maketext (loginactivity.this, password cannot be empty, toast.length_short). Show ();
Return }else{if (box_remeber.ischecked ()) {LoginActivity.this.fileService.saveToRom (name, pass, private).
TXT ");
Toast.maketext (Loginactivity.this, "username and password saved", Toast.length_short). Show ();
}else{Toast.maketext (Loginactivity.this, "username and password do not need to be saved", Toast.length_short). Show ();
}} break;
Default:break;
}/*if (Id==btn_login.getid ()) {String name=edit_name.gettext (). toString ();
String Pass=edit_pass.gettext (). toString ();
if (textutils.isempty (name)) {Toast.maketext (loginactivity.this, "User name cannot be empty", Toast.length_short). Show ();
Return
}else if (Textutils.isempty pass) {toast.maketext (loginactivity.this, password cannot be empty, toast.length_short). Show ();
Return }else{if (box_remeber.ischecked ()) {LoginActivity.this.fileService.saveToRom (name, pass, private). TxT ");
Toast.maketext (Loginactivity.this, "username and password saved", Toast.length_short). Show ();
}else{Toast.maketext (Loginactivity.this, "username and password do not need to be saved", Toast.length_short). Show ();
}
}
}*/
}
}
}
Fileservice.java:
Package com.example.login.service;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.util.HashMap;
Import Java.util.Map;
Import Com.example.login.utils.StreamTools;
Import Android.content.Context;
public class Fileservice {public context context;
Public Fileservice {this.context = context; public boolean savetorom (String name,string pass,string fileName) {try{FileOutputStream Fos=context.open
Fileoutput (FileName, context.mode_private);
String result=name+ ":" +pass;
Fos.write (Result.getbytes ());
Fos.flush ();
Fos.close ();
}catch (Exception e) {e.printstacktrace ();
return false;
return true;
Public map<string,string> readFile (String fileName) {map<string,string> map=null;
try{FileInputStream Fis=context.openfileinput (fileName);
String value=streamtools.getvalue (FIS); String values[]=value.split (":");
if (values.length>0) {map=new hashmap<string, string> ();
Map.put ("name", Values[0]);
Map.put ("Pass", values[1]);
}}catch (Exception e) {e.printstacktrace ();
} return map;
}
}
Streamtools.java:
Package com.example.login.utils;
Import Java.io.ByteArrayOutputStream;
Import Java.io.FileInputStream;
public class Streamtools {public
static String GetValue (FileInputStream fis) throws
exception{ Bytearrayoutputstream stream=new Bytearrayoutputstream ();
Byte[] Buffer=new byte[1024];
int length=-1;
while ((Length=fis.read (buffer))!=-1) {
stream.write (buffer,0,length);
}
Stream.flush ();
Stream.Close ();
String value=stream.tostring ();
return value;
}
}
Login_activity.xml:
<relativelayout 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:paddi" ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ".
Loginactivity "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Wrap_content " Android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" android:orientation= "vertical"
> <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= "ten" Android:inputt Ype= "Textpersonname" > <requestfocus/> </EditText> </LinearLayout> <line
Arlayout 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_cont" Ent "android:text=" @string/text_pass "/> <edittext android:id=" @+id/edit_pass "and Roid:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android: ems= "android:inputtype=" "Textpassword"/> </LinearLayout> <linearlayout android:l Ayout_width= "Match_parenT "android:layout_height=" wrap_content "> <button android:id=" @+id/btn_login "Androi
D:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_weight= "0.17" android:text= "@string/text_login"/> <checkbox android:id= "@+id/cbx_remember" Android:layo Ut_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "80DP" a
ndroid:text= "@string/text_rember"/> </LinearLayout> </LinearLayout> </RelativeLayout>
String.xml:
<?xmlversion= "1.0" encoding= "Utf-8"?>
<resources>
<stringname= "App_name" >login</ string>
<stringname= "action_settings" >Settings</string>
<stringname= "Hello_world" >Login</string>
<stringname= "Text_name" > Username:</string>
<stringname= "Text_pass" > Password:</string>
<stringname= "Text_login" > Landing </string>
<stringname= "Text_rember" > Remember passwords </string>
</resources>
I hope this article will help you with the Android program.