每日日报 登录  File

应用私有路径 data/data/包名/
控件:CheckBox   isChecked();
 
MainActivity.java
  1. public class MainActivity extends Activity {
  2. private EditText et_username;
  3. private EditText et_pwd;
  4. private CheckBox cb_isSave;
  5. private Button btn_login;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. //加载界面
  10. setContentView(R.layout.activity_main);
  11. //找到关心的控件
  12. et_username = (EditText) findViewById(R.id.et_username);
  13. et_pwd = (EditText) findViewById(R.id.et_password);
  14. cb_isSave = (CheckBox) findViewById(R.id.cb_isSave);
  15. btn_login = (Button) findViewById(R.id.btn_login);
  16. //设置点击事件
  17. btn_login.setOnClickListener(new MyListener());
  18. //获取用户保存的信息
  19. // String[] info = Utils.readInfo();
  20. // String[] info = Utils.readInfobyContext(this);
  21. String[] info = Utils.readInfoFromSdCard();
  22. //如果返回不为空 说明有信息 显示到edittext上
  23. if(info!=null){
  24. //显示用户的信息
  25. et_username.setText(info[0]);
  26. et_pwd.setText(info[1]);
  27. }
  28. }
  29. private class MyListener implements OnClickListener{
  30. @Override
  31. public void onClick(View v) {
  32. //当按钮被点击就会走这个方法
  33. //①获取用户输入
  34. String pwd = et_pwd.getText().toString().trim();
  35. String username = et_username.getText().toString().trim();
  36. //②判断输入是否为空
  37. if(TextUtils.isEmpty(username)||TextUtils.isEmpty(pwd)){
  38. //2.1如果为空 Toast提示用户 不能为空
  39. Toast.makeText(MainActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
  40. }else{
  41. //2.2如果不为空 判断是否保存密码
  42. //③ 通过checkbox的状态 判断是否保存
  43. boolean checked = cb_isSave.isChecked();
  44. if(checked){
  45. //boolean saveInfo = Utils.saveInfo(username,pwd);
  46. //boolean saveInfo = Utils.saveInfobycontext(MainActivity.this,username,pwd);
  47. boolean saveInfo = Utils.saveInfo2sdcard(username,pwd);
  48. if(saveInfo){
  49. Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
  50. }else{
  51. Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
  52. }
  53. //勾选上了 保存用户名密码
  54. //Log.d("MainActivity", "保存用户名:"+username+"密码:"+pwd);
  55. }
  56. //④执行登陆的业务逻辑
  57. Log.d("MainActivity", "开始登陆....");
  58. }
  59. }
  60. }
  61. }
Utils.java
  1. /**
  2. * 保存用户名密码
  3. * @param username 用户名
  4. * @param pwd 密码
  5. * @return 是否保存成功
  6. */
  7. public static boolean saveInfo(String username, String pwd) {
  8. String info = username+"##"+pwd;
  9. File file = new File("data/data/com.itheima.logindemo/info.txt");
  10. try {
  11. FileOutputStream fos = new FileOutputStream(file);
  12. fos.write(info.getBytes());
  13. fos.close();
  14. return true;
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. return false;
  18. }
  19. }
  20. /**
  21. * 获取用户保存的用户名和密码
  22. * @return 数组的第一个元素是用户名 第二个元素是密码 如果为null说明获取失败
  23. */
  24. public static String[] readInfo(){
  25. File file = new File("data/data/com.itheima.logindemo/info.txt");
  26. try {
  27. FileInputStream fis = new FileInputStream(file);
  28. BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
  29. String temp = reader.readLine();
  30. String[] result = temp.split("##");
  31. return result;
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. return null;
  35. }
  36. }
布局文件
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <EditText
  11. android:id="@+id/et_username"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:hint="请输入用户名" />
  15. <EditText
  16. android:id="@+id/et_password"
  17. android:layout_below="@id/et_username"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:inputType="textPassword"
  21. android:hint="请输入密码"/>
  22. <CheckBox
  23. android:id="@+id/cb_isSave"
  24. android:layout_below="@id/et_password"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="勾选保存信息"/>
  28. <Button
  29. android:id="@+id/btn_login"
  30. android:layout_below="@id/et_password"
  31. android:layout_alignParentRight="true"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:text="登陆"
  35. />
  36. </RelativeLayout>