• 基础复习——共享参数SharedPreferences——记住密码项目——存储卡的文件操作(读写文件&读写图片)...


    共享参数SharedPreferences——共享参数的用法

    SharedPreferences是Android的一个轻量级存储工具,采用的存储结构是Key-Value的键值对方式。

    共享参数的存储介质是符合XML规范的配置文件。保存路径是:/data/data/应用包名/shared_prefs/文件名.xml

    1. 下面是一个共享参数的XML文件示例:
    2. <map>
    3. <string name="name">Mr Leestring>
    4. <int name="age" value="30" />
    5. <boolean name="married" value="true" />
    6. <float name="weight" value="100.0" />
    7. map>

    共享参数主要适用于如下场合:

    (1)简单且孤立的数据。若是复杂且相互间有关的数据,则要保存在数据库中。

    (2)文本形式的数据。若是二进制数据,则要保存在文件中。

    (3)需要持久化存储的数据。在App退出后再次启动时,之前保存的数据仍然有效。

    实际开发中,共享参数经常存储的数据有App的个性化配置信息、用户使用App的行为信息、临时需要保存的片段信息等。

    下面是使用共享参数的代码例子:

    1. // 从share.xml中获取共享参数对象
    2. SharedPreferences shared = getSharedPreferences("share", MODE_PRIVATE);
    3. // 下面是写入共享参数的代码例子
    4. SharedPreferences.Editor editor = shared.edit(); // 获得编辑器的对象
    5. editor.putString("name", "Mr Lee"); // 添加一个名叫name的字符串参数
    6. editor.putInt("age", 30); // 添加一个名叫age的整型参数
    7. editor.putBoolean("married", true); // 添加一个名叫married的布尔型参数
    8. editor.putFloat("weight", 100f); // 添加一个名叫weight的浮点数参数
    9. editor.commit(); // 提交编辑器中的修改
    10. // 下面是读取共享参数的代码例子
    11. String name = shared.getString("name", ""); // 从共享参数中获得名叫name的字符串
    12. int age = shared.getInt("age", 0); // 从共享参数中获得名叫age的整型数
    13. boolean married = shared.getBoolean("married", false); // 从共享参数中获得名叫married的布尔数
    14. float weight = shared.getFloat("weight", 0); // 从共享参数中获得名叫weight的浮点数

    上一节的实战项目在登录页面下方有一个“记住密码”复选框,现在利用共享参数对该项目进行改造,使之实现记住密码的功能。

    改造的内容主要有3处:

    (1)声明一个共享参数对象,并在onCreate函数中调用getSharedPreferences方法获取共享参数的实例。

    (2)登录成功时,如果用户勾选了“记住密码”,就使用共享参数保存手机号码与密码。

    (3)再次打开登录页面时,App从共享参数中读取手机号码与密码,并展示在界面上。

    1. // 下面是利用共享参数保存密码的代码
    2. // 如果勾选了“记住密码”,则把手机号码和密码都保存到共享参数中
    3. if (bRemember) {
    4. SharedPreferences.Editor editor = mShared.edit(); // 获得编辑器的对象
    5. editor.putString("phone", et_phone.getText().toString()); // 添加名叫phone的手机号码
    6. editor.putString("password", et_password.getText().toString()); // 添加名叫password的密码
    7. editor.commit(); // 提交编辑器中的修改
    8. }
    9. // 下面是利用共享参数读取密码的代码
    10. // 从share.xml中获取共享参数对象
    11. mShared = getSharedPreferences("share_login", MODE_PRIVATE);
    12. // 获取共享参数中保存的手机号码
    13. String phone = mShared.getString("phone", "");
    14. // 获取共享参数中保存的密码
    15. String password = mShared.getString("password", "");
    16. et_phone.setText(phone); // 给手机号码编辑框填写上次保存的手机号
    17. et_password.setText(password); // 给密码编辑框填写上次保存的密码

    布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical"
    5. android:padding="5dp" >
    6. <RelativeLayout
    7. android:layout_width="match_parent"
    8. android:layout_height="40dp" >
    9. <TextView
    10. android:id="@+id/tv_name"
    11. android:layout_width="wrap_content"
    12. android:layout_height="match_parent"
    13. android:gravity="center"
    14. android:text="姓名:"
    15. android:textColor="@color/black"
    16. android:textSize="17sp" />
    17. <EditText
    18. android:id="@+id/et_name"
    19. android:layout_width="match_parent"
    20. android:layout_height="match_parent"
    21. android:layout_marginBottom="3dp"
    22. android:layout_marginTop="3dp"
    23. android:layout_toRightOf="@+id/tv_name"
    24. android:background="@drawable/editext_selector"
    25. android:gravity="left|center"
    26. android:hint="请输入姓名"
    27. android:inputType="text"
    28. android:maxLength="12"
    29. android:textColor="@color/black"
    30. android:textSize="17sp" />
    31. RelativeLayout>
    32. <RelativeLayout
    33. android:layout_width="match_parent"
    34. android:layout_height="40dp" >
    35. <TextView
    36. android:id="@+id/tv_age"
    37. android:layout_width="wrap_content"
    38. android:layout_height="match_parent"
    39. android:gravity="center"
    40. android:text="年龄:"
    41. android:textColor="@color/black"
    42. android:textSize="17sp" />
    43. <EditText
    44. android:id="@+id/et_age"
    45. android:layout_width="match_parent"
    46. android:layout_height="match_parent"
    47. android:layout_marginBottom="3dp"
    48. android:layout_marginTop="3dp"
    49. android:layout_toRightOf="@+id/tv_age"
    50. android:background="@drawable/editext_selector"
    51. android:gravity="left|center"
    52. android:hint="请输入年龄"
    53. android:inputType="number"
    54. android:maxLength="2"
    55. android:textColor="@color/black"
    56. android:textSize="17sp" />
    57. RelativeLayout>
    58. <RelativeLayout
    59. android:layout_width="match_parent"
    60. android:layout_height="40dp" >
    61. <TextView
    62. android:id="@+id/tv_height"
    63. android:layout_width="wrap_content"
    64. android:layout_height="match_parent"
    65. android:gravity="center"
    66. android:text="身高:"
    67. android:textColor="@color/black"
    68. android:textSize="17sp" />
    69. <EditText
    70. android:id="@+id/et_height"
    71. android:layout_width="match_parent"
    72. android:layout_height="match_parent"
    73. android:layout_marginBottom="3dp"
    74. android:layout_marginTop="3dp"
    75. android:layout_toRightOf="@+id/tv_height"
    76. android:background="@drawable/editext_selector"
    77. android:gravity="left|center"
    78. android:hint="请输入身高"
    79. android:inputType="number"
    80. android:maxLength="3"
    81. android:textColor="@color/black"
    82. android:textSize="17sp" />
    83. RelativeLayout>
    84. <RelativeLayout
    85. android:layout_width="match_parent"
    86. android:layout_height="40dp" >
    87. <TextView
    88. android:id="@+id/tv_weight"
    89. android:layout_width="wrap_content"
    90. android:layout_height="match_parent"
    91. android:gravity="center"
    92. android:text="体重:"
    93. android:textColor="@color/black"
    94. android:textSize="17sp" />
    95. <EditText
    96. android:id="@+id/et_weight"
    97. android:layout_width="match_parent"
    98. android:layout_height="match_parent"
    99. android:layout_marginBottom="3dp"
    100. android:layout_marginTop="3dp"
    101. android:layout_toRightOf="@+id/tv_weight"
    102. android:background="@drawable/editext_selector"
    103. android:gravity="left|center"
    104. android:hint="请输入体重"
    105. android:inputType="numberDecimal"
    106. android:maxLength="5"
    107. android:textColor="@color/black"
    108. android:textSize="17sp" />
    109. RelativeLayout>
    110. <RelativeLayout
    111. android:layout_width="match_parent"
    112. android:layout_height="40dp" >
    113. <CheckBox
    114. android:id="@+id/ck_married"
    115. android:layout_width="wrap_content"
    116. android:layout_height="match_parent"
    117. android:gravity="center"
    118. android:checked="false"
    119. android:text="已婚"
    120. android:textColor="@color/black"
    121. android:textSize="17sp" />
    122. RelativeLayout>
    123. <Button
    124. android:id="@+id/btn_save"
    125. android:layout_width="match_parent"
    126. android:layout_height="wrap_content"
    127. android:text="保存到共享参数"
    128. android:textColor="@color/black"
    129. android:textSize="17sp" />
    130. LinearLayout>

    代码:

    1. package com.example.myapplication;
    2. import android.content.SharedPreferences;
    3. import android.os.Bundle;
    4. import android.text.TextUtils;
    5. import android.view.View;
    6. import android.widget.CheckBox;
    7. import android.widget.CompoundButton;
    8. import android.widget.EditText;
    9. import androidx.appcompat.app.AppCompatActivity;
    10. public class ShareWriteActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener
    11. {
    12. private SharedPreferences mShared; // 声明一个共享参数对象
    13. private EditText et_name;
    14. private EditText et_age;
    15. private EditText et_height;
    16. private EditText et_weight;
    17. private boolean isMarried = false;
    18. @Override
    19. protected void onCreate(Bundle savedInstanceState)
    20. {
    21. super.onCreate(savedInstanceState);
    22. setContentView(R.layout.activity_share_write);
    23. et_name = findViewById(R.id.et_name);
    24. et_age = findViewById(R.id.et_age);
    25. et_height = findViewById(R.id.et_height);
    26. et_weight = findViewById(R.id.et_weight);
    27. CheckBox ck_married = findViewById(R.id.ck_married);
    28. ck_married.setOnCheckedChangeListener(this);
    29. findViewById(R.id.btn_save).setOnClickListener(this);
    30. // 从share.xml中获取共享参数对象
    31. mShared = getSharedPreferences("share", MODE_PRIVATE);
    32. }
    33. @Override
    34. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    35. {
    36. isMarried = isChecked;
    37. }
    38. @Override
    39. public void onClick(View v)
    40. {
    41. if (v.getId() == R.id.btn_save)
    42. {
    43. String name = et_name.getText().toString();
    44. String age = et_age.getText().toString();
    45. String height = et_height.getText().toString();
    46. String weight = et_weight.getText().toString();
    47. if (TextUtils.isEmpty(name))
    48. {
    49. ToastUtil.show(this, "请先填写姓名");
    50. return;
    51. }
    52. else if (TextUtils.isEmpty(age))
    53. {
    54. ToastUtil.show(this, "请先填写年龄");
    55. return;
    56. }
    57. else if (TextUtils.isEmpty(height))
    58. {
    59. ToastUtil.show(this, "请先填写身高");
    60. return;
    61. }
    62. else if (TextUtils.isEmpty(weight))
    63. {
    64. ToastUtil.show(this, "请先填写体重");
    65. return;
    66. }
    67. SharedPreferences.Editor editor = mShared.edit(); // 获得编辑器的对象
    68. editor.putString("name", name); // 添加一个名叫name的字符串参数
    69. editor.putInt("age", Integer.parseInt(age)); // 添加一个名叫age的整型参数
    70. editor.putLong("height", Long.parseLong(height)); // 添加一个名叫height的长整型参数
    71. editor.putFloat("weight", Float.parseFloat(weight)); // 添加一个名叫weight的浮点数参数
    72. editor.putBoolean("married", isMarried); // 添加一个名叫married的布尔型参数
    73. editor.putString("update_time", DateUtil.getNowDateTime("yyyy-MM-dd HH:mm:ss"));
    74. editor.commit(); // 提交编辑器中的修改
    75. ToastUtil.show(this, "数据已写入共享参数");
    76. }
    77. }
    78. }

     

    第二个读布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical"
    5. android:padding="5dp" >
    6. <TextView
    7. android:id="@+id/tv_share"
    8. android:layout_width="match_parent"
    9. android:layout_height="wrap_content"
    10. android:textColor="@color/black"
    11. android:textSize="17sp" />
    12. LinearLayout>

    代码:

    1. package com.example.myapplication;
    2. import android.annotation.SuppressLint;
    3. import android.content.SharedPreferences;
    4. import android.os.Bundle;
    5. import android.widget.TextView;
    6. import androidx.appcompat.app.AppCompatActivity;
    7. import java.util.Map;
    8. @SuppressLint("DefaultLocale")
    9. public class ShareReadActivity extends AppCompatActivity
    10. {
    11. private TextView tv_share;
    12. @Override
    13. protected void onCreate(Bundle savedInstanceState)
    14. {
    15. super.onCreate(savedInstanceState);
    16. setContentView(R.layout.activity_share_read);
    17. tv_share = findViewById(R.id.tv_share);
    18. readSharedPreferences(); // 从共享参数中读取信息
    19. }
    20. // 从共享参数中读取信息
    21. private void readSharedPreferences()
    22. {
    23. // 从share.xml中获取共享参数对象
    24. SharedPreferences shared = getSharedPreferences("share", MODE_PRIVATE);
    25. String desc = "共享参数中保存的信息如下:";
    26. // 获取共享参数保存的所有映射配对信息
    27. Map<String, Object> mapParam = (Map<String, Object>) shared.getAll();
    28. // 遍历该映射对象,并将配对信息形成描述文字
    29. for (Map.Entry<String, Object> item_map : mapParam.entrySet())
    30. {
    31. String key = item_map.getKey(); // 获取该配对的键信息
    32. Object value = item_map.getValue(); // 获取该配对的值信息
    33. if (value instanceof String) // 如果配对值的类型为字符串
    34. {
    35. desc = String.format("%s\n %s的取值为%s", desc, key, shared.getString(key, ""));
    36. }
    37. else if (value instanceof Integer) // 如果配对值的类型为整型数
    38. {
    39. desc = String.format("%s\n %s的取值为%d", desc, key, shared.getInt(key, 0));
    40. }
    41. else if (value instanceof Float) // 如果配对值的类型为浮点数
    42. {
    43. desc = String.format("%s\n %s的取值为%f", desc, key, shared.getFloat(key, 0.0f));
    44. }
    45. else if (value instanceof Boolean) // 如果配对值的类型为布尔数
    46. {
    47. desc = String.format("%s\n %s的取值为%b", desc, key, shared.getBoolean(key, false));
    48. }
    49. else if (value instanceof Long) // 如果配对值的类型为长整型
    50. {
    51. desc = String.format("%s\n %s的取值为%d", desc, key, shared.getLong(key, 0L));
    52. }
    53. else // 如果配对值的类型为未知类型
    54. {
    55. desc = String.format("%s\n参数%s的取值为未知类型", desc, key);
    56. }
    57. }
    58. if (mapParam.size() <= 0)
    59. {
    60. desc = "共享参数中保存的信息为空";
    61. }
    62. tv_share.setText(desc);
    63. }
    64. }

     

     

    ToastUtil
    
    1. package com.example.myapplication;
    2. import android.content.Context;
    3. import android.widget.Toast;
    4. public class ToastUtil
    5. {
    6. public static void show(Context ctx, String desc)
    7. {
    8. Toast.makeText(ctx, desc, Toast.LENGTH_SHORT).show();
    9. }
    10. }

    DateUtil
    
    1. package com.example.myapplication;
    2. import android.annotation.SuppressLint;
    3. import android.text.TextUtils;
    4. import java.text.SimpleDateFormat;
    5. import java.util.Date;
    6. @SuppressLint("SimpleDateFormat")
    7. public class DateUtil
    8. {
    9. // 获取当前的日期时间
    10. public static String getNowDateTime(String formatStr)
    11. {
    12. String format = formatStr;
    13. if (TextUtils.isEmpty(format))
    14. {
    15. format = "yyyyMMddHHmmss";
    16. }
    17. SimpleDateFormat sdf = new SimpleDateFormat(format);
    18. return sdf.format(new Date());
    19. }
    20. // 获取当前的时间
    21. public static String getNowTime()
    22. {
    23. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    24. return sdf.format(new Date());
    25. }
    26. // 获取当前的时间(精确到毫秒)
    27. public static String getNowTimeDetail()
    28. {
    29. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
    30. return sdf.format(new Date());
    31. }
    32. }

    editext_selector
    
    1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    2. <item android:state_focused="true" android:drawable="@drawable/shape_edit_focus"/>
    3. <item android:drawable="@drawable/shape_edit_normal"/>
    4. selector>

    shape_edit_focus
    
    1. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    2. <solid android:color="#ffffff" />
    3. <stroke
    4. android:width="2dp"
    5. android:color="#DF162E" />
    6. <corners android:radius="5dp" />
    7. <padding
    8. android:bottom="2dp"
    9. android:left="2dp"
    10. android:right="2dp"
    11. android:top="2dp" />
    12. shape>

    shape_edit_normal
    
    1. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    2. <solid android:color="#ffffff" />
    3. <stroke
    4. android:width="2dp"
    5. android:color="#aaaaaa" />
    6. <corners android:radius="5dp" />
    7. <padding
    8. android:bottom="2dp"
    9. android:left="2dp"
    10. android:right="2dp"
    11. android:top="2dp" />
    12. shape>

     

     

     

    ===========================================================================================================

     布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical"
    5. android:padding="5dp" >
    6. <RadioGroup
    7. android:id="@+id/rg_login"
    8. android:layout_width="match_parent"
    9. android:layout_height="50dp"
    10. android:orientation="horizontal" >
    11. <RadioButton
    12. android:id="@+id/rb_password"
    13. android:layout_width="0dp"
    14. android:layout_height="match_parent"
    15. android:layout_weight="1"
    16. android:checked="true"
    17. android:gravity="left|center"
    18. android:text="密码登录"
    19. android:textColor="@color/black"
    20. android:textSize="17sp" />
    21. <RadioButton
    22. android:id="@+id/rb_verifycode"
    23. android:layout_width="0dp"
    24. android:layout_height="match_parent"
    25. android:layout_weight="1"
    26. android:checked="false"
    27. android:gravity="left|center"
    28. android:text="验证码登录"
    29. android:textColor="@color/black"
    30. android:textSize="17sp" />
    31. RadioGroup>
    32. <RelativeLayout
    33. android:layout_width="match_parent"
    34. android:layout_height="50dp" >
    35. <TextView
    36. android:id="@+id/tv_phone"
    37. android:layout_width="wrap_content"
    38. android:layout_height="match_parent"
    39. android:gravity="center"
    40. android:text="手机号码:"
    41. android:textColor="@color/black"
    42. android:textSize="17sp" />
    43. <EditText
    44. android:id="@+id/et_phone"
    45. android:layout_width="match_parent"
    46. android:layout_height="match_parent"
    47. android:layout_marginBottom="5dp"
    48. android:layout_marginTop="5dp"
    49. android:layout_toRightOf="@+id/tv_phone"
    50. android:background="@drawable/editext_selector"
    51. android:gravity="left|center"
    52. android:hint="请输入手机号码"
    53. android:inputType="number"
    54. android:maxLength="11"
    55. android:textColor="@color/black"
    56. android:textSize="17sp" />
    57. RelativeLayout>
    58. <RelativeLayout
    59. android:layout_width="match_parent"
    60. android:layout_height="50dp" >
    61. <TextView
    62. android:id="@+id/tv_password"
    63. android:layout_width="wrap_content"
    64. android:layout_height="match_parent"
    65. android:gravity="center"
    66. android:text="登录密码:"
    67. android:textColor="@color/black"
    68. android:textSize="17sp" />
    69. <RelativeLayout
    70. android:layout_width="match_parent"
    71. android:layout_height="match_parent"
    72. android:layout_toRightOf="@+id/tv_password" >
    73. <EditText
    74. android:id="@+id/et_password"
    75. android:layout_width="match_parent"
    76. android:layout_height="match_parent"
    77. android:layout_marginBottom="5dp"
    78. android:layout_marginTop="5dp"
    79. android:background="@drawable/editext_selector"
    80. android:gravity="left|center"
    81. android:hint="请输入密码"
    82. android:inputType="numberPassword"
    83. android:maxLength="6"
    84. android:textColor="@color/black"
    85. android:textSize="17sp" />
    86. <Button
    87. android:id="@+id/btn_forget"
    88. android:layout_width="wrap_content"
    89. android:layout_height="match_parent"
    90. android:layout_alignParentRight="true"
    91. android:gravity="center"
    92. android:text="忘记密码"
    93. android:textColor="@color/black"
    94. android:textSize="17sp" />
    95. RelativeLayout>
    96. RelativeLayout>
    97. <CheckBox
    98. android:id="@+id/ck_remember"
    99. android:layout_width="match_parent"
    100. android:layout_height="wrap_content"
    101. android:checked="false"
    102. android:padding="10dp"
    103. android:text="记住密码"
    104. android:textColor="@color/black"
    105. android:textSize="17sp" />
    106. <Button
    107. android:id="@+id/btn_login"
    108. android:layout_width="match_parent"
    109. android:layout_height="wrap_content"
    110. android:text="登  录"
    111. android:textColor="@color/black"
    112. android:textSize="20sp" />
    113. LinearLayout>

     代码:

    1. package com.example.myapplication;
    2. import android.annotation.SuppressLint;
    3. import android.app.AlertDialog;
    4. import android.content.DialogInterface;
    5. import android.content.Intent;
    6. import android.content.SharedPreferences;
    7. import android.os.Bundle;
    8. import android.text.Editable;
    9. import android.text.TextWatcher;
    10. import android.view.View;
    11. import android.widget.Button;
    12. import android.widget.CheckBox;
    13. import android.widget.CompoundButton;
    14. import android.widget.EditText;
    15. import android.widget.RadioButton;
    16. import android.widget.RadioGroup;
    17. import android.widget.TextView;
    18. import android.widget.Toast;
    19. import androidx.appcompat.app.AppCompatActivity;
    20. import java.util.Random;
    21. @SuppressLint("DefaultLocale")
    22. public class LoginShareActivity extends AppCompatActivity implements View.OnClickListener
    23. {
    24. private RadioGroup rg_login; // 声明一个单选组对象
    25. private RadioButton rb_password; // 声明一个单选按钮对象
    26. private RadioButton rb_verifycode; // 声明一个单选按钮对象
    27. private EditText et_phone; // 声明一个编辑框对象
    28. private TextView tv_password; // 声明一个文本视图对象
    29. private EditText et_password; // 声明一个编辑框对象
    30. private Button btn_forget; // 声明一个按钮控件对象
    31. private CheckBox ck_remember; // 声明一个复选框对象
    32. private int mRequestCode = 0; // 跳转页面时的请求代码
    33. private boolean isRemember = false; // 是否记住密码
    34. private String mPassword = "111111"; // 默认密码
    35. private String mVerifyCode; // 验证码
    36. private SharedPreferences mShared; // 声明一个共享参数对象
    37. @Override
    38. protected void onCreate(Bundle savedInstanceState)
    39. {
    40. super.onCreate(savedInstanceState);
    41. setContentView(R.layout.activity_login_share);
    42. rg_login = findViewById(R.id.rg_login);
    43. rb_password = findViewById(R.id.rb_password);
    44. rb_verifycode = findViewById(R.id.rb_verifycode);
    45. et_phone = findViewById(R.id.et_phone);
    46. tv_password = findViewById(R.id.tv_password);
    47. et_password = findViewById(R.id.et_password);
    48. btn_forget = findViewById(R.id.btn_forget);
    49. ck_remember = findViewById(R.id.ck_remember);
    50. // 给rg_login设置单选监听器
    51. rg_login.setOnCheckedChangeListener(new RadioListener());
    52. // 给ck_remember设置勾选监听器
    53. ck_remember.setOnCheckedChangeListener(new CheckListener());
    54. // 给et_phone添加文本变更监听器
    55. et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11));
    56. // 给et_password添加文本变更监听器
    57. et_password.addTextChangedListener(new HideTextWatcher(et_password, 11));
    58. btn_forget.setOnClickListener(this);
    59. findViewById(R.id.btn_login).setOnClickListener(this);
    60. // 从share_login.xml获取共享参数对象
    61. mShared = getSharedPreferences("share_login", MODE_PRIVATE);
    62. // 获取共享参数保存的手机号码
    63. String phone = mShared.getString("phone", "");
    64. // 获取共享参数保存的密码
    65. String password = mShared.getString("password", "");
    66. et_phone.setText(phone); // 往手机号码编辑框填写上次保存的手机号
    67. et_password.setText(password); // 往密码编辑框填写上次保存的密码
    68. }
    69. // 定义登录方式的单选监听器
    70. private class RadioListener implements RadioGroup.OnCheckedChangeListener
    71. {
    72. @Override
    73. public void onCheckedChanged(RadioGroup group, int checkedId)
    74. {
    75. if (checkedId == R.id.rb_password) // 选择了密码登录
    76. {
    77. tv_password.setText("登录密码:");
    78. et_password.setHint("请输入密码");
    79. btn_forget.setText("忘记密码");
    80. ck_remember.setVisibility(View.VISIBLE);
    81. }
    82. else if (checkedId == R.id.rb_verifycode) // 选择了验证码登录
    83. {
    84. tv_password.setText(" 验证码:");
    85. et_password.setHint("请输入验证码");
    86. btn_forget.setText("获取验证码");
    87. ck_remember.setVisibility(View.GONE);
    88. }
    89. }
    90. }
    91. // 定义是否记住密码的勾选监听器
    92. private class CheckListener implements CompoundButton.OnCheckedChangeListener
    93. {
    94. @Override
    95. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    96. {
    97. if (buttonView.getId() == R.id.ck_remember)
    98. {
    99. isRemember = isChecked;
    100. }
    101. }
    102. }
    103. // 定义一个编辑框监听器,在输入文本达到指定长度时自动隐藏输入法
    104. private class HideTextWatcher implements TextWatcher
    105. {
    106. private EditText mView; // 声明一个编辑框对象
    107. private int mMaxLength; // 声明一个最大长度变量
    108. public HideTextWatcher(EditText v, int maxLength)
    109. {
    110. super();
    111. mView = v;
    112. mMaxLength = maxLength;
    113. }
    114. // 在编辑框的输入文本变化前触发
    115. public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    116. // 在编辑框的输入文本变化时触发
    117. public void onTextChanged(CharSequence s, int start, int before, int count) {}
    118. // 在编辑框的输入文本变化后触发
    119. public void afterTextChanged(Editable s)
    120. {
    121. String str = s.toString(); // 获得已输入的文本字符串
    122. // 输入文本达到11位(如手机号码),或者达到6位(如登录密码)时关闭输入法
    123. if ((str.length() == 11 && mMaxLength == 11)
    124. || (str.length() == 6 && mMaxLength == 6))
    125. {
    126. ViewUtil.hideOneInputMethod(LoginShareActivity.this, mView); // 隐藏输入法软键盘
    127. }
    128. }
    129. }
    130. @Override
    131. public void onClick(View v)
    132. {
    133. String phone = et_phone.getText().toString();
    134. if (v.getId() == R.id.btn_forget) { // 点击了“忘记密码”按钮
    135. if (phone.length() < 11) { // 手机号码不足11位
    136. Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
    137. return;
    138. }
    139. if (rb_password.isChecked()) { // 选择了密码方式校验,此时要跳到找回密码页面
    140. // 以下携带手机号码跳转到找回密码页面
    141. Intent intent = new Intent(this, LoginForgetActivity.class);
    142. intent.putExtra("phone", phone);
    143. startActivityForResult(intent, mRequestCode); // 携带意图返回上一个页面
    144. } else if (rb_verifycode.isChecked()) { // 选择了验证码方式校验,此时要生成六位随机数字验证码
    145. // 生成六位随机数字的验证码
    146. mVerifyCode = String.format("%06d", new Random().nextInt(999999));
    147. // 以下弹出提醒对话框,提示用户记住六位验证码数字
    148. AlertDialog.Builder builder = new AlertDialog.Builder(this);
    149. builder.setTitle("请记住验证码");
    150. builder.setMessage("手机号" + phone + ",本次验证码是" + mVerifyCode + ",请输入验证码");
    151. builder.setPositiveButton("好的", null);
    152. AlertDialog alert = builder.create();
    153. alert.show(); // 显示提醒对话框
    154. }
    155. } else if (v.getId() == R.id.btn_login) { // 点击了“登录”按钮
    156. if (phone.length() < 11) { // 手机号码不足11位
    157. Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
    158. return;
    159. }
    160. if (rb_password.isChecked()) { // 密码方式校验
    161. if (!et_password.getText().toString().equals(mPassword)) {
    162. Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
    163. } else { // 密码校验通过
    164. loginSuccess(); // 提示用户登录成功
    165. }
    166. } else if (rb_verifycode.isChecked()) { // 验证码方式校验
    167. if (!et_password.getText().toString().equals(mVerifyCode)) {
    168. Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
    169. } else { // 验证码校验通过
    170. loginSuccess(); // 提示用户登录成功
    171. }
    172. }
    173. }
    174. }
    175. // 从下一个页面携带参数返回当前页面时触发
    176. @Override
    177. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    178. super.onActivityResult(requestCode, resultCode, data);
    179. if (requestCode == mRequestCode && data != null) {
    180. // 用户密码已改为新密码,故更新密码变量
    181. mPassword = data.getStringExtra("new_password");
    182. }
    183. }
    184. // 从修改密码页面返回登录页面,要清空密码的输入框
    185. @Override
    186. protected void onRestart()
    187. {
    188. super.onRestart();
    189. et_password.setText("");
    190. }
    191. // 校验通过,登录成功
    192. private void loginSuccess()
    193. {
    194. String desc = String.format("您的手机号码是%s,恭喜你通过登录验证,点击“确定”按钮返回上个页面", et_phone.getText().toString());
    195. // 以下弹出提醒对话框,提示用户登录成功
    196. AlertDialog.Builder builder = new AlertDialog.Builder(this);
    197. builder.setTitle("登录成功");
    198. builder.setMessage(desc);
    199. builder.setPositiveButton("确定返回", new DialogInterface.OnClickListener() {
    200. @Override
    201. public void onClick(DialogInterface dialog, int which) {
    202. finish(); // 结束当前的活动页面
    203. }
    204. });
    205. builder.setNegativeButton("我再看看", null);
    206. AlertDialog alert = builder.create();
    207. alert.show(); // 显示提醒对话框
    208. // 如果勾选了“记住密码”,就把手机号码和密码都保存到共享参数中
    209. if (isRemember)
    210. {
    211. SharedPreferences.Editor editor = mShared.edit(); // 获得编辑器的对象
    212. editor.putString("phone", et_phone.getText().toString()); // 添加名叫phone的手机号码
    213. editor.putString("password", et_password.getText().toString()); // 添加名叫password的密码
    214. editor.commit(); // 提交编辑器中的修改
    215. }
    216. }
    217. }

     

     

     

     

     

     

    第二个布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical"
    5. android:padding="5dp" >
    6. <RelativeLayout
    7. android:layout_width="match_parent"
    8. android:layout_height="50dp" >
    9. <TextView
    10. android:id="@+id/tv_password_first"
    11. android:layout_width="wrap_content"
    12. android:layout_height="match_parent"
    13. android:gravity="center"
    14. android:text="输入新密码:"
    15. android:textColor="@color/black"
    16. android:textSize="17sp" />
    17. <EditText
    18. android:id="@+id/et_password_first"
    19. android:layout_width="match_parent"
    20. android:layout_height="match_parent"
    21. android:layout_marginBottom="5dp"
    22. android:layout_marginTop="5dp"
    23. android:layout_toRightOf="@+id/tv_password_first"
    24. android:background="@drawable/editext_selector"
    25. android:gravity="left|center"
    26. android:hint="请输入新密码"
    27. android:inputType="numberPassword"
    28. android:maxLength="11"
    29. android:textColor="@color/black"
    30. android:textSize="17sp" />
    31. RelativeLayout>
    32. <RelativeLayout
    33. android:layout_width="match_parent"
    34. android:layout_height="50dp" >
    35. <TextView
    36. android:id="@+id/tv_password_second"
    37. android:layout_width="wrap_content"
    38. android:layout_height="match_parent"
    39. android:gravity="center"
    40. android:text="确认新密码:"
    41. android:textColor="@color/black"
    42. android:textSize="17sp" />
    43. <EditText
    44. android:id="@+id/et_password_second"
    45. android:layout_width="match_parent"
    46. android:layout_height="match_parent"
    47. android:layout_marginBottom="5dp"
    48. android:layout_marginTop="5dp"
    49. android:layout_toRightOf="@+id/tv_password_second"
    50. android:background="@drawable/editext_selector"
    51. android:gravity="left|center"
    52. android:hint="请再次输入新密码"
    53. android:inputType="numberPassword"
    54. android:maxLength="11"
    55. android:textColor="@color/black"
    56. android:textSize="17sp" />
    57. RelativeLayout>
    58. <RelativeLayout
    59. android:layout_width="match_parent"
    60. android:layout_height="50dp" >
    61. <TextView
    62. android:id="@+id/tv_verifycode"
    63. android:layout_width="wrap_content"
    64. android:layout_height="match_parent"
    65. android:gravity="center"
    66. android:text="  验证码:"
    67. android:textColor="@color/black"
    68. android:textSize="17sp" />
    69. <RelativeLayout
    70. android:layout_width="match_parent"
    71. android:layout_height="match_parent"
    72. android:layout_toRightOf="@+id/tv_verifycode" >
    73. <EditText
    74. android:id="@+id/et_verifycode"
    75. android:layout_width="match_parent"
    76. android:layout_height="match_parent"
    77. android:layout_marginBottom="5dp"
    78. android:layout_marginTop="5dp"
    79. android:background="@drawable/editext_selector"
    80. android:gravity="left|center"
    81. android:hint="请输入验证码"
    82. android:inputType="numberPassword"
    83. android:maxLength="6"
    84. android:textColor="@color/black"
    85. android:textSize="17sp" />
    86. <Button
    87. android:id="@+id/btn_verifycode"
    88. android:layout_width="wrap_content"
    89. android:layout_height="match_parent"
    90. android:layout_alignParentRight="true"
    91. android:gravity="center"
    92. android:text="获取验证码"
    93. android:textColor="@color/black"
    94. android:textSize="17sp" />
    95. RelativeLayout>
    96. RelativeLayout>
    97. <Button
    98. android:id="@+id/btn_confirm"
    99. android:layout_width="match_parent"
    100. android:layout_height="wrap_content"
    101. android:text="确  定"
    102. android:textColor="@color/black"
    103. android:textSize="20sp" />
    104. LinearLayout>

     代码:

    1. package com.example.myapplication;
    2. import android.annotation.SuppressLint;
    3. import android.app.Activity;
    4. import android.app.AlertDialog;
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.widget.EditText;
    9. import android.widget.Toast;
    10. import androidx.appcompat.app.AppCompatActivity;
    11. import java.util.Random;
    12. @SuppressLint("DefaultLocale")
    13. public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener
    14. {
    15. private EditText et_password_first; // 声明一个编辑框对象
    16. private EditText et_password_second; // 声明一个编辑框对象
    17. private EditText et_verifycode; // 声明一个编辑框对象
    18. private String mVerifyCode; // 验证码
    19. private String mPhone; // 手机号码
    20. @Override
    21. protected void onCreate(Bundle savedInstanceState)
    22. {
    23. super.onCreate(savedInstanceState);
    24. setContentView(R.layout.activity_login_forget);
    25. // 从布局文件中获取名叫et_password_first的编辑框
    26. et_password_first = findViewById(R.id.et_password_first);
    27. // 从布局文件中获取名叫et_password_second的编辑框
    28. et_password_second = findViewById(R.id.et_password_second);
    29. // 从布局文件中获取名叫et_verifycode的编辑框
    30. et_verifycode = findViewById(R.id.et_verifycode);
    31. findViewById(R.id.btn_verifycode).setOnClickListener(this);
    32. findViewById(R.id.btn_confirm).setOnClickListener(this);
    33. // 从上一个页面获取要修改密码的手机号码
    34. mPhone = getIntent().getStringExtra("phone");
    35. }
    36. @Override
    37. public void onClick(View v)
    38. {
    39. if (v.getId() == R.id.btn_verifycode) // 点击了“获取验证码”按钮
    40. {
    41. if (mPhone == null || mPhone.length() < 11)
    42. {
    43. Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
    44. return;
    45. }
    46. // 生成六位随机数字的验证码
    47. mVerifyCode = String.format("%06d", new Random().nextInt(999999));
    48. // 以下弹出提醒对话框,提示用户记住六位验证码数字
    49. AlertDialog.Builder builder = new AlertDialog.Builder(this);
    50. builder.setTitle("请记住验证码");
    51. builder.setMessage("手机号" + mPhone + ",本次验证码是" + mVerifyCode + ",请输入验证码");
    52. builder.setPositiveButton("好的", null);
    53. AlertDialog alert = builder.create();
    54. alert.show(); // 显示提醒对话框
    55. }
    56. else if (v.getId() == R.id.btn_confirm) // 点击了“确定”按钮
    57. {
    58. String password_first = et_password_first.getText().toString();
    59. String password_second = et_password_second.getText().toString();
    60. if (password_first.length() < 6 || password_second.length() < 6)
    61. {
    62. Toast.makeText(this, "请输入正确的新密码", Toast.LENGTH_SHORT).show();
    63. return;
    64. }
    65. if (!password_first.equals(password_second))
    66. {
    67. Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
    68. return;
    69. }
    70. if (!et_verifycode.getText().toString().equals(mVerifyCode))
    71. {
    72. Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
    73. }
    74. else
    75. {
    76. Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
    77. // 以下把修改好的新密码返回给上一个页面
    78. Intent intent = new Intent(); // 创建一个新意图
    79. intent.putExtra("new_password", password_first); // 存入新密码
    80. setResult(Activity.RESULT_OK, intent); // 携带意图返回上一个页面
    81. finish(); // 结束当前的活动页面
    82. }
    83. }
    84. }
    85. }

     

     

     

     

    ============================================================================================================

     

     私有存储空间与公共存储空间

    Android把外部存储分成了两块区域,一块是所有应用均可访问的公共空间,另一块是只有应用自己才可访问的私有空间。

    Android在SD卡的“Android/data”目录下给每个应用又单独建了一个文件目录,用于给应用保存自己需要处理的临时文件。

    这个给每个应用单独建立的文件目录,只有当前应用才能够读写文件,其它应用是不允许进行读写的,故而“Android/data”目录算是外部存储上的私有空间。

    Android从7.0开始加强了SD卡的权限管理,App使用SD卡的公共控件前既需要事先声明权限,又需要在设置页面开启权限,使用私有空间无需另外设置权限。

     获取公共空间的存储路径,调用的是Environment类的getExternalStoragePublicDirectory方法;

    1. 获取应用私有空间的存储路径,调用的是getExternalFilesDir方法。代码如下:
    2. // 获取系统的公共存储路径
    3. String publicPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
    4. // 获取当前App的私有存储路径
    5. String privatePath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();
    6. TextView tv_file_path = findViewById(R.id.tv_file_path);
    7. String desc = "系统的公共存储路径位于" + publicPath +"\n\n当前App的私有存储路径位于" + privatePath;
    8. tv_file_path.setText(desc);

    文本文件的读写一般借助于FileOutputStream和FileInputStream。(1)FileOutputStream用于写文件。(2)FileInputStream用于读文件。

    布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical"
    5. android:padding="5dp" >
    6. <TextView
    7. android:id="@+id/tv_path"
    8. android:layout_width="match_parent"
    9. android:layout_height="wrap_content"
    10. android:textColor="@color/black"
    11. android:textSize="17sp" />
    12. LinearLayout>

     

     代码:

    1. package com.example.myapplication;
    2. import android.os.Build;
    3. import android.os.Bundle;
    4. import android.os.Environment;
    5. import android.widget.TextView;
    6. import androidx.appcompat.app.AppCompatActivity;
    7. public class FilePathActivity extends AppCompatActivity
    8. {
    9. @Override
    10. protected void onCreate(Bundle savedInstanceState)
    11. {
    12. super.onCreate(savedInstanceState);
    13. setContentView(R.layout.activity_file_path);
    14. TextView tv_path = findViewById(R.id.tv_path);
    15. // Android7.0之后默认禁止访问公共存储目录
    16. // 获取系统的公共存储路径
    17. String publicPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
    18. // 获取当前App的私有存储路径
    19. String privatePath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();
    20. boolean isLegacy = true;
    21. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
    22. {
    23. // Android10的存储空间默认采取分区方式,此处判断是传统方式还是分区方式
    24. isLegacy = Environment.isExternalStorageLegacy();
    25. }
    26. String desc = "系统的公共存储路径位于" + publicPath +
    27. "\n\n当前App的私有存储路径位于" + privatePath +
    28. "\n\nAndroid7.0之后默认禁止访问公共存储目录" +
    29. "\n\n当前App的存储空间采取" + (isLegacy?"传统方式":"分区方式");
    30. tv_path.setText(desc);
    31. }
    32. }

     

     

     存储卡上读写文本文件

     布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical"
    5. android:padding="5dp" >
    6. <RelativeLayout
    7. android:layout_width="match_parent"
    8. android:layout_height="40dp" >
    9. <TextView
    10. android:id="@+id/tv_name"
    11. android:layout_width="wrap_content"
    12. android:layout_height="match_parent"
    13. android:gravity="center"
    14. android:text="姓名:"
    15. android:textColor="@color/black"
    16. android:textSize="17sp" />
    17. <EditText
    18. android:id="@+id/et_name"
    19. android:layout_width="match_parent"
    20. android:layout_height="match_parent"
    21. android:layout_marginBottom="3dp"
    22. android:layout_marginTop="3dp"
    23. android:layout_toRightOf="@+id/tv_name"
    24. android:background="@drawable/editext_selector"
    25. android:gravity="left|center"
    26. android:hint="请输入姓名"
    27. android:inputType="text"
    28. android:maxLength="12"
    29. android:textColor="@color/black"
    30. android:textSize="17sp" />
    31. RelativeLayout>
    32. <RelativeLayout
    33. android:layout_width="match_parent"
    34. android:layout_height="40dp" >
    35. <TextView
    36. android:id="@+id/tv_age"
    37. android:layout_width="wrap_content"
    38. android:layout_height="match_parent"
    39. android:gravity="center"
    40. android:text="年龄:"
    41. android:textColor="@color/black"
    42. android:textSize="17sp" />
    43. <EditText
    44. android:id="@+id/et_age"
    45. android:layout_width="match_parent"
    46. android:layout_height="match_parent"
    47. android:layout_marginBottom="3dp"
    48. android:layout_marginTop="3dp"
    49. android:layout_toRightOf="@+id/tv_age"
    50. android:background="@drawable/editext_selector"
    51. android:gravity="left|center"
    52. android:hint="请输入年龄"
    53. android:inputType="number"
    54. android:maxLength="2"
    55. android:textColor="@color/black"
    56. android:textSize="17sp" />
    57. RelativeLayout>
    58. <RelativeLayout
    59. android:layout_width="match_parent"
    60. android:layout_height="40dp" >
    61. <TextView
    62. android:id="@+id/tv_height"
    63. android:layout_width="wrap_content"
    64. android:layout_height="match_parent"
    65. android:gravity="center"
    66. android:text="身高:"
    67. android:textColor="@color/black"
    68. android:textSize="17sp" />
    69. <EditText
    70. android:id="@+id/et_height"
    71. android:layout_width="match_parent"
    72. android:layout_height="match_parent"
    73. android:layout_marginBottom="3dp"
    74. android:layout_marginTop="3dp"
    75. android:layout_toRightOf="@+id/tv_height"
    76. android:background="@drawable/editext_selector"
    77. android:gravity="left|center"
    78. android:hint="请输入身高"
    79. android:inputType="number"
    80. android:maxLength="3"
    81. android:textColor="@color/black"
    82. android:textSize="17sp" />
    83. RelativeLayout>
    84. <RelativeLayout
    85. android:layout_width="match_parent"
    86. android:layout_height="40dp" >
    87. <TextView
    88. android:id="@+id/tv_weight"
    89. android:layout_width="wrap_content"
    90. android:layout_height="match_parent"
    91. android:gravity="center"
    92. android:text="体重:"
    93. android:textColor="@color/black"
    94. android:textSize="17sp" />
    95. <EditText
    96. android:id="@+id/et_weight"
    97. android:layout_width="match_parent"
    98. android:layout_height="match_parent"
    99. android:layout_marginBottom="3dp"
    100. android:layout_marginTop="3dp"
    101. android:layout_toRightOf="@+id/tv_weight"
    102. android:background="@drawable/editext_selector"
    103. android:gravity="left|center"
    104. android:hint="请输入体重"
    105. android:inputType="numberDecimal"
    106. android:maxLength="5"
    107. android:textColor="@color/black"
    108. android:textSize="17sp" />
    109. RelativeLayout>
    110. <RelativeLayout
    111. android:layout_width="match_parent"
    112. android:layout_height="40dp" >
    113. <CheckBox
    114. android:id="@+id/ck_married"
    115. android:layout_width="wrap_content"
    116. android:layout_height="match_parent"
    117. android:gravity="center"
    118. android:checked="false"
    119. android:text="已婚"
    120. android:textColor="@color/black"
    121. android:textSize="17sp" />
    122. RelativeLayout>
    123. <Button
    124. android:id="@+id/btn_save"
    125. android:layout_width="match_parent"
    126. android:layout_height="wrap_content"
    127. android:text="保存文本到存储卡"
    128. android:textColor="@color/black"
    129. android:textSize="17sp" />
    130. <TextView
    131. android:id="@+id/tv_path"
    132. android:layout_width="wrap_content"
    133. android:layout_height="match_parent"
    134. android:textColor="@color/black"
    135. android:textSize="17sp" />
    136. LinearLayout>

    editext_selector
    
    1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    2. <item android:state_focused="true" android:drawable="@drawable/shape_edit_focus"/>
    3. <item android:drawable="@drawable/shape_edit_normal"/>
    4. selector>

    shape_edit_focus.xml
    
    1. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    2. <solid android:color="#ffffff" />
    3. <stroke
    4. android:width="2dp"
    5. android:color="#DF162E" />
    6. <corners android:radius="5dp" />
    7. <padding
    8. android:bottom="2dp"
    9. android:left="2dp"
    10. android:right="2dp"
    11. android:top="2dp" />
    12. shape>

    shape_edit_normal
    
    1. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    2. <solid android:color="#ffffff" />
    3. <stroke
    4. android:width="2dp"
    5. android:color="#aaaaaa" />
    6. <corners android:radius="5dp" />
    7. <padding
    8. android:bottom="2dp"
    9. android:left="2dp"
    10. android:right="2dp"
    11. android:top="2dp" />
    12. shape>

    代码:

    1. package com.example.myapplication;
    2. import android.annotation.SuppressLint;
    3. import android.os.Bundle;
    4. import android.os.Environment;
    5. import android.text.TextUtils;
    6. import android.view.View;
    7. import android.widget.CheckBox;
    8. import android.widget.CompoundButton;
    9. import android.widget.EditText;
    10. import android.widget.TextView;
    11. import androidx.appcompat.app.AppCompatActivity;
    12. @SuppressLint("SetTextI18n")
    13. public class FileWriteActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener
    14. {
    15. private EditText et_name;
    16. private EditText et_age;
    17. private EditText et_height;
    18. private EditText et_weight;
    19. private boolean isMarried = false;
    20. private String[] typeArray = {"未婚", "已婚"};
    21. private String mPath; // 私有目录路径
    22. private TextView tv_path;
    23. @Override
    24. protected void onCreate(Bundle savedInstanceState)
    25. {
    26. super.onCreate(savedInstanceState);
    27. setContentView(R.layout.activity_file_write);
    28. et_name = findViewById(R.id.et_name);
    29. et_age = findViewById(R.id.et_age);
    30. et_height = findViewById(R.id.et_height);
    31. et_weight = findViewById(R.id.et_weight);
    32. tv_path = findViewById(R.id.tv_path);
    33. CheckBox ck_married = findViewById(R.id.ck_married);
    34. ck_married.setOnCheckedChangeListener(this);
    35. findViewById(R.id.btn_save).setOnClickListener(this);
    36. // 获取当前App的私有下载目录
    37. mPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
    38. }
    39. @Override
    40. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    41. {
    42. isMarried = isChecked;
    43. }
    44. @Override
    45. public void onClick(View v)
    46. {
    47. if (v.getId() == R.id.btn_save)
    48. {
    49. String name = et_name.getText().toString();
    50. String age = et_age.getText().toString();
    51. String height = et_height.getText().toString();
    52. String weight = et_weight.getText().toString();
    53. if (TextUtils.isEmpty(name))
    54. {
    55. ToastUtil.show(this, "请先填写姓名");
    56. return;
    57. }
    58. else if (TextUtils.isEmpty(age))
    59. {
    60. ToastUtil.show(this, "请先填写年龄");
    61. return;
    62. }
    63. else if (TextUtils.isEmpty(height))
    64. {
    65. ToastUtil.show(this, "请先填写身高");
    66. return;
    67. }
    68. else if (TextUtils.isEmpty(weight))
    69. {
    70. ToastUtil.show(this, "请先填写体重");
    71. return;
    72. }
    73. String content = String.format(" 姓名:%s\n 年龄:%s\n 身高:%scm\n 体重:%skg\n 婚否:%s\n 注册时间:%s\n",
    74. name, age, height, weight, typeArray[isMarried?1:0], DateUtil.getNowDateTime("yyyy-MM-dd HH:mm:ss"));
    75. String file_path = mPath + DateUtil.getNowDateTime("") + ".txt";
    76. FileUtil.saveText(file_path, content); // 把字符串内容保存为文本文件
    77. tv_path.setText("用户注册信息文件的保存路径为:\n" + file_path);
    78. ToastUtil.show(this, "数据已写入存储卡文件");
    79. }
    80. }
    81. }

     

     

    第二个布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical"
    5. android:padding="5dp" >
    6. <Button
    7. android:id="@+id/btn_delete"
    8. android:layout_width="match_parent"
    9. android:layout_height="wrap_content"
    10. android:text="删除所有文本文件"
    11. android:textColor="@color/black"
    12. android:textSize="17sp" />
    13. <TextView
    14. android:id="@+id/tv_content"
    15. android:layout_width="match_parent"
    16. android:layout_height="wrap_content"
    17. android:textColor="@color/black"
    18. android:textSize="17sp" />
    19. LinearLayout>

    代码:

    1. package com.example.myapplication;
    2. import android.annotation.SuppressLint;
    3. import android.os.Bundle;
    4. import android.os.Environment;
    5. import android.util.Log;
    6. import android.view.View;
    7. import android.widget.TextView;
    8. import androidx.appcompat.app.AppCompatActivity;
    9. import java.io.File;
    10. import java.util.ArrayList;
    11. import java.util.List;
    12. @SuppressLint("SetTextI18n")
    13. public class FileReadActivity extends AppCompatActivity implements View.OnClickListener
    14. {
    15. private final static String TAG = "FileReadActivity";
    16. private TextView tv_content;
    17. private String mPath; // 私有目录路径
    18. private List mFilelist = new ArrayList();
    19. @Override
    20. protected void onCreate(Bundle savedInstanceState)
    21. {
    22. super.onCreate(savedInstanceState);
    23. setContentView(R.layout.activity_file_read);
    24. tv_content = findViewById(R.id.tv_content);
    25. findViewById(R.id.btn_delete).setOnClickListener(this);
    26. // 获取当前App的私有下载目录
    27. mPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
    28. showFileContent(); // 显示最新的文本文件内容
    29. }
    30. // 显示最新的文本文件内容
    31. private void showFileContent()
    32. {
    33. // 获得指定目录下面的所有文本文件
    34. mFilelist = FileUtil.getFileList(mPath, new String[]{".txt"});
    35. if (mFilelist.size() > 0)
    36. {
    37. // 打开并显示选中的文本文件内容
    38. String file_path = mFilelist.get(0).getAbsolutePath();
    39. String content = FileUtil.openText(file_path);
    40. String desc = String.format("找到最新的文本文件,路径为%s,内容如下:\n%s",
    41. file_path, content);
    42. tv_content.setText(desc);
    43. }
    44. else
    45. {
    46. tv_content.setText("私有目录下未找到任何文本文件");
    47. }
    48. }
    49. @Override
    50. public void onClick(View v)
    51. {
    52. if (v.getId() == R.id.btn_delete)
    53. {
    54. for (int i = 0; i < mFilelist.size(); i++)
    55. {
    56. String file_path = mFilelist.get(i).getAbsolutePath();
    57. File f = new File(file_path);
    58. if (!f.delete())
    59. {
    60. Log.d(TAG, "file_path=" + file_path + ", delete failed");
    61. }
    62. }
    63. ToastUtil.show(this, "已删除私有目录下的所有文本文件");
    64. }
    65. }
    66. }

     

     

    FileUtil
    
    1. package com.example.myapplication;
    2. import android.graphics.Bitmap;
    3. import android.graphics.BitmapFactory;
    4. import java.io.File;
    5. import java.io.FileInputStream;
    6. import java.io.FileOutputStream;
    7. import java.io.FilenameFilter;
    8. import java.util.ArrayList;
    9. import java.util.Collections;
    10. import java.util.Comparator;
    11. import java.util.List;
    12. import java.util.Locale;
    13. public class FileUtil
    14. {
    15. // 把字符串保存到指定路径的文本文件
    16. public static void saveText(String path, String txt)
    17. {
    18. // 根据指定的文件路径构建文件输出流对象
    19. try (FileOutputStream fos = new FileOutputStream(path))
    20. {
    21. fos.write(txt.getBytes()); // 把字符串写入文件输出流
    22. }
    23. catch (Exception e)
    24. {
    25. e.printStackTrace();
    26. }
    27. }
    28. // 从指定路径的文本文件中读取内容字符串
    29. public static String openText(String path)
    30. {
    31. String readStr = "";
    32. // 根据指定的文件路径构建文件输入流对象
    33. try (FileInputStream fis = new FileInputStream(path))
    34. {
    35. byte[] b = new byte[fis.available()];
    36. fis.read(b); // 从文件输入流读取字节数组
    37. readStr = new String(b); // 把字节数组转换为字符串
    38. }
    39. catch (Exception e)
    40. {
    41. e.printStackTrace();
    42. }
    43. return readStr; // 返回文本文件中的文本字符串
    44. }
    45. // 把位图数据保存到指定路径的图片文件
    46. public static void saveImage(String path, Bitmap bitmap)
    47. {
    48. // 根据指定的文件路径构建文件输出流对象
    49. try (FileOutputStream fos = new FileOutputStream(path))
    50. {
    51. // 把位图数据压缩到文件输出流中
    52. bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
    53. }
    54. catch (Exception e)
    55. {
    56. e.printStackTrace();
    57. }
    58. }
    59. // 从指定路径的图片文件中读取位图数据
    60. public static Bitmap openImage(String path)
    61. {
    62. Bitmap bitmap = null; // 声明一个位图对象
    63. // 根据指定的文件路径构建文件输入流对象
    64. try (FileInputStream fis = new FileInputStream(path))
    65. {
    66. bitmap = BitmapFactory.decodeStream(fis); // 从文件输入流中解码位图数据
    67. }
    68. catch (Exception e)
    69. {
    70. e.printStackTrace();
    71. }
    72. return bitmap; // 返回图片文件中的位图数据
    73. }
    74. public static List getFileList(String path, String[] extendArray)
    75. {
    76. List displayedContent = new ArrayList();
    77. File[] files = null;
    78. File directory = new File(path);
    79. if (extendArray != null && extendArray.length > 0)
    80. {
    81. FilenameFilter fileFilter = getTypeFilter(extendArray);
    82. files = directory.listFiles(fileFilter);
    83. }
    84. else
    85. {
    86. files = directory.listFiles();
    87. }
    88. if (files != null)
    89. {
    90. for (File f : files)
    91. {
    92. if (!f.isDirectory() && !f.isHidden())
    93. {
    94. displayedContent.add(f);
    95. }
    96. }
    97. }
    98. // 按照最后修改时间排序
    99. Collections.sort(displayedContent, new Comparator()
    100. {
    101. @Override
    102. public int compare(File o1, File o2)
    103. {
    104. return (o1.lastModified() > o2.lastModified()) ? -1 : 1;
    105. }
    106. });
    107. return displayedContent;
    108. }
    109. public static FilenameFilter getTypeFilter(String[] extendArray)
    110. {
    111. final ArrayList fileExtensions = new ArrayList();
    112. for (int i = 0; i < extendArray.length; i++)
    113. {
    114. fileExtensions.add(extendArray[i]);
    115. }
    116. FilenameFilter fileNameFilter = new FilenameFilter()
    117. {
    118. @Override
    119. public boolean accept(File directory, String fileName)
    120. {
    121. boolean matched = false;
    122. File f = new File(String.format("%s/%s", directory.getAbsolutePath(), fileName));
    123. matched = f.isDirectory();
    124. if (!matched)
    125. {
    126. for (String s : fileExtensions)
    127. {
    128. s = String.format(".{0,}\\%s$", s);
    129. s = s.toUpperCase(Locale.getDefault());
    130. fileName = fileName.toUpperCase(Locale.getDefault());
    131. matched = fileName.matches(s);
    132. if (matched)
    133. {
    134. break;
    135. }
    136. }
    137. }
    138. return matched;
    139. }
    140. };
    141. return fileNameFilter;
    142. }
    143. }
    ToastUtil
    
    1. package com.example.myapplication;
    2. import android.content.Context;
    3. import android.widget.Toast;
    4. public class ToastUtil
    5. {
    6. public static void show(Context ctx, String desc)
    7. {
    8. Toast.makeText(ctx, desc, Toast.LENGTH_SHORT).show();
    9. }
    10. }
    ViewUtil
    
    1. package com.example.myapplication;
    2. import android.app.Activity;
    3. import android.content.Context;
    4. import android.view.View;
    5. import android.view.inputmethod.InputMethodManager;
    6. public class ViewUtil
    7. {
    8. public static void hideAllInputMethod(Activity act)
    9. {
    10. // 从系统服务中获取输入法管理器
    11. InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
    12. if (imm.isActive()) // 软键盘如果已经打开则关闭之
    13. {
    14. imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    15. }
    16. }
    17. public static void hideOneInputMethod(Activity act, View v)
    18. {
    19. // 从系统服务中获取输入法管理器
    20. InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
    21. // 关闭屏幕上的输入法软键盘
    22. imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    23. }
    24. }

     

     

    ==================================================================================================

    在存储卡上读写图片

     布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical"
    5. android:padding="5dp" >
    6. <ImageView
    7. android:id="@+id/iv_content"
    8. android:layout_width="match_parent"
    9. android:layout_height="250dp"
    10. android:scaleType="fitCenter" />
    11. <Button
    12. android:id="@+id/btn_save"
    13. android:layout_width="match_parent"
    14. android:layout_height="wrap_content"
    15. android:text="把资源图片保存到存储卡"
    16. android:textColor="@color/black"
    17. android:textSize="17sp" />
    18. <TextView
    19. android:id="@+id/tv_path"
    20. android:layout_width="wrap_content"
    21. android:layout_height="match_parent"
    22. android:textColor="@color/black"
    23. android:textSize="17sp" />
    24. LinearLayout>

    代码:

    1. package com.example.myapplication;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.graphics.Bitmap;
    4. import android.graphics.BitmapFactory;
    5. import android.os.Bundle;
    6. import android.os.Environment;
    7. import android.view.View;
    8. import android.widget.ImageView;
    9. import android.widget.TextView;
    10. public class ImageWriteActivity extends AppCompatActivity implements View.OnClickListener
    11. {
    12. private ImageView iv_content;
    13. private TextView tv_path;
    14. @Override
    15. protected void onCreate(Bundle savedInstanceState)
    16. {
    17. super.onCreate(savedInstanceState);
    18. setContentView(R.layout.activity_image_write);
    19. iv_content = findViewById(R.id.iv_content);
    20. iv_content.setImageResource(R.drawable.huawei); // 设置图像视图的图片资源
    21. tv_path = findViewById(R.id.tv_path);
    22. findViewById(R.id.btn_save).setOnClickListener(this);
    23. }
    24. @Override
    25. public void onClick(View v)
    26. {
    27. if (v.getId() == R.id.btn_save)
    28. {
    29. // 获取当前App的私有下载目录
    30. String path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
    31. // 从指定的资源文件中获取位图对象
    32. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.huawei);
    33. String file_path = path + DateUtil.getNowDateTime("") + ".jpeg";
    34. FileUtil.saveImage(file_path, bitmap); // 把位图对象保存为图片文件
    35. tv_path.setText("图片文件的保存路径为:\n" + file_path);
    36. ToastUtil.show(this, "图片已写入存储卡文件");
    37. }
    38. }
    39. }

     

    布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical"
    5. android:padding="5dp" >
    6. <Button
    7. android:id="@+id/btn_delete"
    8. android:layout_width="match_parent"
    9. android:layout_height="wrap_content"
    10. android:text="删除所有图片文件"
    11. android:textColor="@color/black"
    12. android:textSize="17sp" />
    13. <TextView
    14. android:id="@+id/tv_content"
    15. android:layout_width="match_parent"
    16. android:layout_height="wrap_content"
    17. android:textColor="@color/black"
    18. android:textSize="17sp" />
    19. <ImageView
    20. android:id="@+id/iv_content"
    21. android:layout_width="match_parent"
    22. android:layout_height="250dp"
    23. android:scaleType="fitCenter" />
    24. LinearLayout>

    代码:

    1. package com.example.myapplication;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.graphics.Bitmap;
    4. import android.os.Bundle;
    5. import android.os.Environment;
    6. import android.util.Log;
    7. import android.view.View;
    8. import android.widget.ImageView;
    9. import android.widget.TextView;
    10. import java.io.File;
    11. import java.util.ArrayList;
    12. import java.util.List;
    13. public class ImageReadActivity extends AppCompatActivity implements View.OnClickListener
    14. {
    15. private final static String TAG = "ImageReadActivity";
    16. private TextView tv_content;
    17. private ImageView iv_content;
    18. private String mPath; // 私有目录路径
    19. private List mFilelist = new ArrayList();
    20. @Override
    21. protected void onCreate(Bundle savedInstanceState)
    22. {
    23. super.onCreate(savedInstanceState);
    24. setContentView(R.layout.activity_image_read);
    25. tv_content = findViewById(R.id.tv_content);
    26. iv_content = findViewById(R.id.iv_content);
    27. findViewById(R.id.btn_delete).setOnClickListener(this);
    28. // 获取当前App的私有下载目录
    29. mPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
    30. showFileContent(); // 显示最新的图片文件内容
    31. }
    32. // 显示最新的图片文件内容
    33. private void showFileContent()
    34. {
    35. // 获得指定目录下面的所有图片文件
    36. mFilelist = FileUtil.getFileList(mPath, new String[]{".jpeg"});
    37. if (mFilelist.size() > 0)
    38. {
    39. // 打开并显示选中的图片文件内容
    40. String file_path = mFilelist.get(0).getAbsolutePath();
    41. tv_content.setText("找到最新的图片文件,路径为"+file_path);
    42. // 显示存储卡图片文件的第一种方式:直接调用setImageURI方法
    43. //iv_content.setImageURI(Uri.parse(file_path)); // 设置图像视图的路径对象
    44. // 第二种方式:先调用BitmapFactory.decodeFile获得位图,再调用setImageBitmap方法
    45. //Bitmap bitmap = BitmapFactory.decodeFile(file_path);
    46. //iv_content.setImageBitmap(bitmap); // 设置图像视图的位图对象
    47. // 第三种方式:先调用FileUtil.openImage获得位图,再调用setImageBitmap方法
    48. Bitmap bitmap = FileUtil.openImage(file_path);
    49. iv_content.setImageBitmap(bitmap); // 设置图像视图的位图对象
    50. }
    51. else
    52. {
    53. tv_content.setText("私有目录下未找到任何图片文件");
    54. }
    55. }
    56. @Override
    57. public void onClick(View v)
    58. {
    59. if (v.getId() == R.id.btn_delete)
    60. {
    61. for (int i = 0; i < mFilelist.size(); i++)
    62. {
    63. // 获取该文件的绝对路径字符串
    64. String file_path = mFilelist.get(i).getAbsolutePath();
    65. File f = new File(file_path);
    66. if (!f.delete()) // 删除文件,并判断是否成功删除
    67. {
    68. Log.d(TAG, "file_path=" + file_path + ", delete failed");
    69. }
    70. }
    71. ToastUtil.show(this, "已删除私有目录下的所有图片文件");
    72. }
    73. }
    74. }

     

     

     

     

     

     

     

     

     

     

    PS:

    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    2. package="com.example.chapter06">
    3. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    4. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    5. <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    6. <application
    7. android:name=".MainApplication"
    8. android:allowBackup="true"
    9. android:icon="@mipmap/ic_launcher"
    10. android:label="@string/app_name"
    11. android:roundIcon="@mipmap/ic_launcher_round"
    12. android:supportsRtl="true"
    13. android:theme="@style/AppTheme">
    14. <activity android:name=".MainActivity">
    15. <intent-filter>
    16. <action android:name="android.intent.action.MAIN" />
    17. <category android:name="android.intent.category.LAUNCHER" />
    18. intent-filter>
    19. activity>
    20. <activity android:name=".ShareWriteActivity" />
    21. <activity android:name=".ShareReadActivity" />
    22. <activity android:name=".LoginShareActivity" />
    23. <activity android:name=".LoginForgetActivity" />
    24. <activity android:name=".DatabaseActivity" />
    25. <activity android:name=".SQLiteWriteActivity" />
    26. <activity android:name=".SQLiteReadActivity" />
    27. <activity android:name=".LoginSQLiteActivity" />
    28. <activity android:name=".FilePathActivity" />
    29. <activity android:name=".FileWriteActivity" />
    30. <activity android:name=".FileReadActivity" />
    31. <activity android:name=".ImageWriteActivity" />
    32. <activity android:name=".ImageReadActivity" />
    33. <activity android:name=".ActTestActivity" />
    34. <activity android:name=".AppWriteActivity" />
    35. <activity android:name=".AppReadActivity" />
    36. <activity android:name=".RoomWriteActivity" />
    37. <activity android:name=".RoomReadActivity" />
    38. <activity
    39. android:name=".ShoppingCartActivity"
    40. android:theme="@style/AppCompatTheme" />
    41. <activity
    42. android:name=".ShoppingChannelActivity"
    43. android:theme="@style/AppCompatTheme" />
    44. <activity
    45. android:name=".ShoppingDetailActivity"
    46. android:theme="@style/AppCompatTheme" />
    47. application>
    48. manifest>
  • 相关阅读:
    java4.23学习总结
    创建型模式-建造者模式(四)
    设计模式之桥梁模式
    CentOS Nginx crontab 定时执行PHP代码命令任务(tp5.1 tp6 EasyTask) 每天重启执行PHP自定义命令
    【openGauss】运维常用的SQL
    搭建rtmp流媒体服务器的步骤
    使用ssl_certificate_by_lua指令动态加载证书
    如何有效对直流列头柜进行监测
    Redis配置文件
    力扣由浅至深 每日一题.04 最长公共前缀
  • 原文地址:https://blog.csdn.net/m0_61442607/article/details/126215125