功能要求 : 用户名zhangsan 密码 123 为正确登录状态
如果勾选记住密码 会在登录之前保存 用户名密码到应用程序 /files/infx.txt文件中
存储格式为 用户名##密码
接下来 是 xml 代码
<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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入用户名" /> <EditText android:id="@+id/et_username" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入密码 " /> <EditText android:id="@+id/et_password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPassword" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="记住密码" /> <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="登录" /> </RelativeLayout> </LinearLayout>
接下来是mainactivity 代码
package com.liwei.loginview;
import java.util.HashMap;
import java.util.Map;
import com.liwei.loginview.services.LoginService;
import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private EditText et_username;//用户名
private EditText et_password;//密码
private CheckBox ch;//check
private Button button;//登录按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
ch = (CheckBox) findViewById(R.id.check);
button = (Button) findViewById(R.id.login);
//判断 是否有保存记录 如果不为空的话 取出显示到界面上
Map<String, String> map = new HashMap<String, String>();
map = LoginService.getSavedUserInfo(this);
if (map != null) {
et_username.setText(map.get("username"));
et_password.setText(map.get("password"));
}
button.setOnClickListener(this);
// 检查是否有保存的用户名密码 如果有 显示出来
}
@Override
public void onClick(View v) {
String name = et_username.getText().toString();
String pwd = et_password.getText().toString();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
Toast.makeText(this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
} else {
// 判断是否保存密码
if (ch.isChecked()) {
// 保存用户密码
boolean ruslt = LoginService.saveUserInfo(this, name, pwd);
if (ruslt) {
Log.i("TAG", "保存成功");
}
Log.i("TAG", "保存密码成功");
Toast.makeText(this, "保存密码成功", Toast.LENGTH_SHORT).show();
}
if ("zhangsan".equals(name) && "123".equals(pwd)) {
Log.i("TAG", "登陆成功");
Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
} else {
Log.i("TAG", "登陆失败");
Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
}
}
保存文件 读取文件业务方法
package com.liwei.loginview.services;
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.nio.Buffer;
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("/data/data/com.liwei.loginview/info.txt");
File file = new File(context.getFilesDir(), "info.txt"); //获取路径 如 "/data/data/com.liwei.loginview/files / 创建文件 info.txt
// context.getFilesDir();//返回一个目录/data/data/com.liwei.loginview+
// files
FileOutputStream fos = new FileOutputStream(file);
// zhangsan ## 123
fos.write((username + "##" + password).getBytes());
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static Map<String, String> getSavedUserInfo(Context context) {
File file = new File(context.getFilesDir(), "info.txt");
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String str = br.readLine();
String[] infos = str.split("##");
Map<String, String> map = new HashMap<String, String>();
map.put("username", infos[0]);
map.put("password", infos[1]);
return map;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
接下来附上源代码 loginview.zip