Android开发之完成登陆界面的数据保存回显操作实例
本文实例讲述了Android开发之完成登陆界面的数据保存回显操作。分享给大家供大家参考,具体如下:
LoginActivity.java:
packagecom.example.login;
importjava.util.Map;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.text.TextUtils;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.CheckBox;
importandroid.widget.EditText;
importandroid.widget.Toast;
importcom.example.login.service.FileService;
publicclassLoginActivityextendsActivity{
publicEditTextedit_name,edit_pass;
publicButtonbtn_login;
publicCheckBoxbox_remeber;
publicFileServicefileService;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
fileService=newFileService(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(newMyOnClickListener());
Map<String,String>map=fileService.readFile("private.txt");
if(map!=null){
edit_name.setText(map.get("name"));
edit_pass.setText(map.get("pass"));
}
}
@Override
publicbooleanonCreateOptionsMenu(Menumenu){
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate(R.menu.login,menu);
returntrue;
}
classMyOnClickListenerimplementsView.OnClickListener{
@Override
publicvoidonClick(Viewv){
intid=v.getId();
switch(id){
caseR.id.btn_login:
Stringname=edit_name.getText().toString();
Stringpass=edit_pass.getText().toString();
if(TextUtils.isEmpty(name)){
Toast.makeText(LoginActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
return;
}elseif(TextUtils.isEmpty(pass)){
Toast.makeText(LoginActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
return;
}else{
if(box_remeber.isChecked()){
LoginActivity.this.fileService.saveToRom(name,pass,"private.txt");
Toast.makeText(LoginActivity.this,"用户名和密码已保存",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LoginActivity.this,"用户名和密码不需要保存",Toast.LENGTH_SHORT).show();
}
}
break;
default:
break;
}
/*if(id==btn_login.getId()){
Stringname=edit_name.getText().toString();
Stringpass=edit_pass.getText().toString();
if(TextUtils.isEmpty(name)){
Toast.makeText(LoginActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
return;
}elseif(TextUtils.isEmpty(pass)){
Toast.makeText(LoginActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
return;
}else{
if(box_remeber.isChecked()){
LoginActivity.this.fileService.saveToRom(name,pass,"private.txt");
Toast.makeText(LoginActivity.this,"用户名和密码已保存",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LoginActivity.this,"用户名和密码不需要保存",Toast.LENGTH_SHORT).show();
}
}
}*/
}
}
}
FileService.java:
packagecom.example.login.service;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.util.HashMap;
importjava.util.Map;
importcom.example.login.utils.StreamTools;
importandroid.content.Context;
publicclassFileService{
publicContextcontext;
publicFileService(Contextcontext){
this.context=context;
}
publicbooleansaveToRom(Stringname,Stringpass,StringfileName){
try{
FileOutputStreamfos=context.openFileOutput(fileName,Context.MODE_PRIVATE);
Stringresult=name+":"+pass;
fos.write(result.getBytes());
fos.flush();
fos.close();
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
returntrue;
}
publicMap<String,String>readFile(StringfileName){
Map<String,String>map=null;
try{
FileInputStreamfis=context.openFileInput(fileName);
Stringvalue=StreamTools.getValue(fis);
Stringvalues[]=value.split(":");
if(values.length>0){
map=newHashMap<String,String>();
map.put("name",values[0]);
map.put("pass",values[1]);
}
}catch(Exceptione){
e.printStackTrace();
}
returnmap;
}
}
StreamTools.java:
packagecom.example.login.utils;
importjava.io.ByteArrayOutputStream;
importjava.io.FileInputStream;
publicclassStreamTools{
publicstaticStringgetValue(FileInputStreamfis)throwsException{
ByteArrayOutputStreamstream=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlength=-1;
while((length=fis.read(buffer))!=-1){
stream.write(buffer,0,length);
}
stream.flush();
stream.close();
Stringvalue=stream.toString();
returnvalue;
}
}
login_activity.xml:
<RelativeLayoutxmlns: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=".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="10" android:inputType="textPersonName"> <requestFocus/> </EditText> </LinearLayout> <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"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn_login" android: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:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="80dp" android: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">用户名:</string> <stringname="text_pass">密码:</string> <stringname="text_login">登陆</string> <stringname="text_rember">记住密码</string> </resources>
希望本文所述对大家Android程序设计有所帮助。