一、File: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: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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="请输入用户名" />
<EditText
android:id="@+id/et_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/et_username"
android:layout_below="@+id/et_username"
android:layout_marginTop="20dp"
android:text="请输入用户密码" />
<EditText
android:id="@+id/ed_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<CheckBox
android:id="@+id/checkbox_remeber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/btn_login"
android:text="记住我" />
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/et_username"
android:layout_below="@+id/ed_password"
android:layout_marginTop="27dp"
android:gravity="left"
android:onClick="login"
android:text="登陆" />
</RelativeLayout>
效果图:
二、
File: MainActivity.java
package com.jiangge.login;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.jiangge.login.service.LoginService;
import com.jiangge.logindemo.R;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private EditText mUsername;
private EditText mPassword;
private CheckBox mRemember;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsername = (EditText) findViewById(R.id.et_username);
mPassword = (EditText) findViewById(R.id.ed_password);
mRemember = (CheckBox) findViewById(R.id.checkbox_remeber);
Map<String, String> map = LoginService.getSavedUserInfo(this);
if (map != null) {
mUsername.setText(map.get("username"));
mPassword.setText(map.get("password"));
}
}
public void login(View view) {
String username = mUsername.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_LONG).show();
}
if (mRemember.isChecked()) {
//保存用户名和密码
Log.i(TAG, "保存用户名和密码");
boolean result = LoginService.saveUserInfo(this, username, password);
if (result) {
Toast.makeText(this, "保存用户名和密码成功", Toast.LENGTH_LONG).show();
}
}
//登录发送到服务器,服务器验证是否正确。模拟。
if ("jiangge".equals(username) && "123".equals(password)) {
Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();
}
}
}
File: LoginService.java
package com.jiangge.login.service;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
public class LoginService {
//存数据
public static boolean saveUserInfo(Context context, String username, String password){
try {
File file = new File(context.getFilesDir(), "info.txt"); //Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead
System.out.println("====>>>"+ context.getFilesDir());// /data/data/com.jiangge.logindemo/files
FileOutputStream fos = new FileOutputStream(file);
fos.write((username + "##" + password).getBytes());
fos.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
//取数据
public static Map<String,String> getSavedUserInfo(Context context){
try {
File file = new File(context.getFilesDir(), "info.txt");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String str = br.readLine();
br.close();
String [] infos = str.split("##"); //jiangge##123
Map<String,String> map = new HashMap<String, String>();
map.put("username", infos[0]);
map.put("password", infos[1]);
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
注:
保存文件:
File file = new File(context.getFilesDir(), "info.txt"); //Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead
System.out.println("====>>>"+ context.getFilesDir());// /data/data/com.jiangge.logindemo/files
FileOutputStream fos = new FileOutputStream(file);
fos.write((username + "##" + password).getBytes());
fos.close();
也可以将以上的核心代码,使用Activity提供的方法 openFileOutput(String name, int mode)
FileOutputStream fos = context.openFileOutput("private.txt", Context.MODE_PRIVATE);
fos.write((username + "##" + password).getBytes());
fos.close();
保存到了 /data/data/包名/files/目录下