布局文件:
<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"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入QQ号" />
<EditText
android:id="@+id/et_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/cb_remerber_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="记住密码" />
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
Utils类:
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
//保存(获取)用户信息到内置存储空间
/**
* 保存用户信息
*
* @param number
* @param password
* @return true 成功
*/
public static boolean saveUserInfo(Context context, String number, String password) {
try {
File filesDir = context.getFilesDir();
// File filesDir = context.getCacheDir();
File f = new File(filesDir, "user.txt");
FileOutputStream fos = new FileOutputStream(f);
String data = number + "##" + password;
fos.write(data.getBytes());
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 返回用户信息
*
* @return
*/
public static Map<String, String> getUserInfo(Context context) {
try {
File filesDir = context.getFilesDir();
// File filesDir = context.getCacheDir();
File f = new File(filesDir, "user.txt");
FileInputStream fis = new FileInputStream(f);
// 字符流对象
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String text = reader.readLine();
if (!TextUtils.isEmpty(text)) {
String[] split = text.split("##");
Map<String, String> userInfoMap = new HashMap<String, String>();
userInfoMap.put("number", split[0]);
userInfoMap.put("password", split[1]);
return userInfoMap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
//保存(获取)用户信息到SD卡
/**
* 保存用户信息到sd卡
*
* @param number
* @param password
* @return true 成功
*/
public static boolean saveUserInfoOfSDCard(Context context, String number, String password) {
try {
// 判断当前的手机是否有sd卡
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
// 已经挂载了sd卡
return false;
}
File sdCardFile = Environment.getExternalStorageDirectory();
File file = new File(sdCardFile, "user.txt");
FileOutputStream fos = new FileOutputStream(file);
String data = number + "##" + password;
fos.write(data.getBytes());
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 到sd卡获取用户信息
*
* @return
*/
public static Map<String, String> getUserInfoOfSDCard(Context context) {
try {
// 判断当前的手机是否有sd卡
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
// 已经挂载了sd卡
return null;
}
File sdCardFile = Environment.getExternalStorageDirectory();
File file = new File(sdCardFile, "user.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String text = br.readLine();
br.close();
if (!TextUtils.isEmpty(text)) {
Map<String, String> userInfoMap = new HashMap<String, String>();
String[] split = text.split("##");
userInfoMap.put("number", split[0]);
userInfoMap.put("password", split[1]);
return userInfoMap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//
Activity类:
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
private EditText etNumber;
private EditText etPassword;
private CheckBox cbRemerberPWD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNumber = (EditText) findViewById(R.id.et_number);
etPassword = (EditText) findViewById(R.id.et_password);
cbRemerberPWD = (CheckBox) findViewById(R.id.cb_remerber_pwd);
Button btnLogin = (Button) findViewById(R.id.btn_login);
btnLogin.setOnClickListener(this);
// 回显数据
Map<String, String> userInfoMap = Utils.getUserInfo(this);
// Map<String, String> userInfoMap = Utils.getUserInfoOfSDCard(this);
if (userInfoMap != null) {
etNumber.setText(userInfoMap.get("number"));
etPassword.setText(userInfoMap.get("password"));
}
}
@Override
public void onClick(View v) {
// 执行登录的操作
// 1. 取出号码和密码
String number = etNumber.getText().toString();
String password = etPassword.getText().toString();
if (TextUtils.isEmpty(number) || TextUtils.isEmpty(password)) {
// 弹出吐司
Toast.makeText(this, "请正确输入", Toast.LENGTH_SHORT).show();
return;
}
// 2. 判断记住密码是否被选中, 如果被选中, 存起来
if (cbRemerberPWD.isChecked()) {
// 当前需要记住密码
Log.i(TAG, "记住密码: " + number + ", " + password);
boolean isSuccess = Utils.saveUserInfo(this, number, password);
// boolean isSuccess = Utils.saveUserInfoOfSDCard(this, number, password);
if (isSuccess) {
Toast.makeText(this, "保存成功", 0).show();
} else {
Toast.makeText(this, "保存失败", 0).show();
}
}
// 3. 登陆成功
Toast.makeText(this, "登录成功", 0).show();
}
}
注意:操作SD卡文件时,需要在AndroidManifest.xml文件中配置:<!-- 写入sd卡的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />