点击记住密码,保存账号密码。
1.获取各数据的对象
2.判断记住密码是否被选中,如果被选中,存起来
3.登录成功
显示界面:
<span style="font-size:14px;"><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" >
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入QQ号"
/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码"/>
<CheckBox
android:id="@+id/cb_remember_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="记住密码"/>
<!-- checkedtrue代表选中 -->
<Button
android:id="@+id/btn_login"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="登录"/>
</LinearLayout>
</span>
Activity:
<span style="font-size:14px;">package com.itheima28.qqlogin;
import java.util.Map;
import com.itheima28.qqlogin.utils.Utils;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
private EditText etNumber;
private EditText etPassword;
private CheckBox cbRemerberPWD;
private Map<String, String> userInfoMap;
@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_remember_pwd);
Button btnLogin = (Button) findViewById(R.id.btn_login);
btnLogin.setOnClickListener(this);
userInfoMap = Utils.getUserInfo();
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--弹出吐司,下面的业务就不执行了
Toast.makeText(this, "请正确输入", Toast.LENGTH_SHORT).show();
/*
第一个参数为Content对象,MainActivity继承Content,所以用this就行了
第二个参数时候提示的信息
第三个参数持续的时间 Toast.LENGTH_SHORT--0秒 Toast.LENGTH_LONG--1秒
*/
}
//2.判断记住密码是否被选中,如果被选中,存起来
if(cbRemerberPWD.isChecked()){
//当前需要记住密码
Log.i(TAG, "记住密码:"+number + ","+password);
boolean isSuccess = Utils.saveUserInfo(number, password);
if(isSuccess){
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
//3.登录成功
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}
}
</span>
工具类:
<span style="font-size:14px;">package com.itheima28.qqlogin.utils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import android.text.TextUtils;
public class Utils {
/*
* 保存存户信息
* */
public static boolean saveUserInfo(String number,String password){
try {
String path = "/data/data/com.itheima28.qqlogin/itheima28.txt";
FileOutputStream fos = new FileOutputStream(path);
//存储12345##6545564
String date = number + "##" + password;
fos.write(date.getBytes());
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 返回用户信息
* */
public static Map<String,String> getUserInfo(){
try {
String path = "/data/data/com.itheima28.qqlogin/itheima28.txt";
FileInputStream fis = new FileInputStream(path);
//字符流对象
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("number", split[1]);
return userInfoMap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
</span>