共享参数SharedPreferences——共享参数的用法
SharedPreferences是Android的一个轻量级存储工具,采用的存储结构是Key-Value的键值对方式。
共享参数的存储介质是符合XML规范的配置文件。保存路径是:/data/data/应用包名/shared_prefs/文件名.xml
- 下面是一个共享参数的XML文件示例:
- <map>
- <string name="name">Mr Leestring>
- <int name="age" value="30" />
- <boolean name="married" value="true" />
- <float name="weight" value="100.0" />
- map>

共享参数主要适用于如下场合:
(1)简单且孤立的数据。若是复杂且相互间有关的数据,则要保存在数据库中。
(2)文本形式的数据。若是二进制数据,则要保存在文件中。
(3)需要持久化存储的数据。在App退出后再次启动时,之前保存的数据仍然有效。
实际开发中,共享参数经常存储的数据有App的个性化配置信息、用户使用App的行为信息、临时需要保存的片段信息等。
下面是使用共享参数的代码例子:
- // 从share.xml中获取共享参数对象
- SharedPreferences shared = getSharedPreferences("share", MODE_PRIVATE);
-
- // 下面是写入共享参数的代码例子
- SharedPreferences.Editor editor = shared.edit(); // 获得编辑器的对象
- editor.putString("name", "Mr Lee"); // 添加一个名叫name的字符串参数
- editor.putInt("age", 30); // 添加一个名叫age的整型参数
- editor.putBoolean("married", true); // 添加一个名叫married的布尔型参数
- editor.putFloat("weight", 100f); // 添加一个名叫weight的浮点数参数
- editor.commit(); // 提交编辑器中的修改
-
- // 下面是读取共享参数的代码例子
- String name = shared.getString("name", ""); // 从共享参数中获得名叫name的字符串
- int age = shared.getInt("age", 0); // 从共享参数中获得名叫age的整型数
- boolean married = shared.getBoolean("married", false); // 从共享参数中获得名叫married的布尔数
- float weight = shared.getFloat("weight", 0); // 从共享参数中获得名叫weight的浮点数

上一节的实战项目在登录页面下方有一个“记住密码”复选框,现在利用共享参数对该项目进行改造,使之实现记住密码的功能。
改造的内容主要有3处:
(1)声明一个共享参数对象,并在onCreate函数中调用getSharedPreferences方法获取共享参数的实例。
(2)登录成功时,如果用户勾选了“记住密码”,就使用共享参数保存手机号码与密码。
(3)再次打开登录页面时,App从共享参数中读取手机号码与密码,并展示在界面上。
- // 下面是利用共享参数保存密码的代码
- // 如果勾选了“记住密码”,则把手机号码和密码都保存到共享参数中
- if (bRemember) {
- SharedPreferences.Editor editor = mShared.edit(); // 获得编辑器的对象
- editor.putString("phone", et_phone.getText().toString()); // 添加名叫phone的手机号码
- editor.putString("password", et_password.getText().toString()); // 添加名叫password的密码
- editor.commit(); // 提交编辑器中的修改
- }
-
- // 下面是利用共享参数读取密码的代码
- // 从share.xml中获取共享参数对象
- mShared = getSharedPreferences("share_login", MODE_PRIVATE);
- // 获取共享参数中保存的手机号码
- String phone = mShared.getString("phone", "");
- // 获取共享参数中保存的密码
- String password = mShared.getString("password", "");
- et_phone.setText(phone); // 给手机号码编辑框填写上次保存的手机号
- et_password.setText(password); // 给密码编辑框填写上次保存的密码

布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="5dp" >
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="40dp" >
-
- <TextView
- android:id="@+id/tv_name"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="姓名:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <EditText
- android:id="@+id/et_name"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="3dp"
- android:layout_marginTop="3dp"
- android:layout_toRightOf="@+id/tv_name"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入姓名"
- android:inputType="text"
- android:maxLength="12"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="40dp" >
-
- <TextView
- android:id="@+id/tv_age"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="年龄:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <EditText
- android:id="@+id/et_age"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="3dp"
- android:layout_marginTop="3dp"
- android:layout_toRightOf="@+id/tv_age"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入年龄"
- android:inputType="number"
- android:maxLength="2"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="40dp" >
-
- <TextView
- android:id="@+id/tv_height"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="身高:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <EditText
- android:id="@+id/et_height"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="3dp"
- android:layout_marginTop="3dp"
- android:layout_toRightOf="@+id/tv_height"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入身高"
- android:inputType="number"
- android:maxLength="3"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="40dp" >
-
- <TextView
- android:id="@+id/tv_weight"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="体重:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <EditText
- android:id="@+id/et_weight"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="3dp"
- android:layout_marginTop="3dp"
- android:layout_toRightOf="@+id/tv_weight"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入体重"
- android:inputType="numberDecimal"
- android:maxLength="5"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="40dp" >
-
- <CheckBox
- android:id="@+id/ck_married"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:checked="false"
- android:text="已婚"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <Button
- android:id="@+id/btn_save"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="保存到共享参数"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- LinearLayout>

代码:
- package com.example.myapplication;
-
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.EditText;
- import androidx.appcompat.app.AppCompatActivity;
-
- public class ShareWriteActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener
- {
- private SharedPreferences mShared; // 声明一个共享参数对象
-
- private EditText et_name;
- private EditText et_age;
- private EditText et_height;
- private EditText et_weight;
- private boolean isMarried = false;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_share_write);
-
-
- et_name = findViewById(R.id.et_name);
- et_age = findViewById(R.id.et_age);
- et_height = findViewById(R.id.et_height);
- et_weight = findViewById(R.id.et_weight);
-
- CheckBox ck_married = findViewById(R.id.ck_married);
-
- ck_married.setOnCheckedChangeListener(this);
- findViewById(R.id.btn_save).setOnClickListener(this);
-
- // 从share.xml中获取共享参数对象
- mShared = getSharedPreferences("share", MODE_PRIVATE);
- }
-
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
- {
- isMarried = isChecked;
- }
-
- @Override
- public void onClick(View v)
- {
- if (v.getId() == R.id.btn_save)
- {
- String name = et_name.getText().toString();
- String age = et_age.getText().toString();
- String height = et_height.getText().toString();
- String weight = et_weight.getText().toString();
-
- if (TextUtils.isEmpty(name))
- {
- ToastUtil.show(this, "请先填写姓名");
- return;
- }
- else if (TextUtils.isEmpty(age))
- {
- ToastUtil.show(this, "请先填写年龄");
- return;
- }
- else if (TextUtils.isEmpty(height))
- {
- ToastUtil.show(this, "请先填写身高");
- return;
- }
- else if (TextUtils.isEmpty(weight))
- {
- ToastUtil.show(this, "请先填写体重");
- return;
- }
-
- SharedPreferences.Editor editor = mShared.edit(); // 获得编辑器的对象
-
- editor.putString("name", name); // 添加一个名叫name的字符串参数
- editor.putInt("age", Integer.parseInt(age)); // 添加一个名叫age的整型参数
- editor.putLong("height", Long.parseLong(height)); // 添加一个名叫height的长整型参数
- editor.putFloat("weight", Float.parseFloat(weight)); // 添加一个名叫weight的浮点数参数
- editor.putBoolean("married", isMarried); // 添加一个名叫married的布尔型参数
- editor.putString("update_time", DateUtil.getNowDateTime("yyyy-MM-dd HH:mm:ss"));
-
- editor.commit(); // 提交编辑器中的修改
-
- ToastUtil.show(this, "数据已写入共享参数");
- }
- }
-
- }



第二个读布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="5dp" >
-
- <TextView
- android:id="@+id/tv_share"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- LinearLayout>

代码:
- package com.example.myapplication;
-
- import android.annotation.SuppressLint;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.widget.TextView;
- import androidx.appcompat.app.AppCompatActivity;
- import java.util.Map;
-
- @SuppressLint("DefaultLocale")
- public class ShareReadActivity extends AppCompatActivity
- {
- private TextView tv_share;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_share_read);
-
-
- tv_share = findViewById(R.id.tv_share);
- readSharedPreferences(); // 从共享参数中读取信息
- }
-
- // 从共享参数中读取信息
- private void readSharedPreferences()
- {
- // 从share.xml中获取共享参数对象
- SharedPreferences shared = getSharedPreferences("share", MODE_PRIVATE);
-
- String desc = "共享参数中保存的信息如下:";
-
- // 获取共享参数保存的所有映射配对信息
- Map<String, Object> mapParam = (Map<String, Object>) shared.getAll();
-
- // 遍历该映射对象,并将配对信息形成描述文字
- for (Map.Entry<String, Object> item_map : mapParam.entrySet())
- {
- String key = item_map.getKey(); // 获取该配对的键信息
-
- Object value = item_map.getValue(); // 获取该配对的值信息
-
- if (value instanceof String) // 如果配对值的类型为字符串
- {
- desc = String.format("%s\n %s的取值为%s", desc, key, shared.getString(key, ""));
- }
- else if (value instanceof Integer) // 如果配对值的类型为整型数
- {
- desc = String.format("%s\n %s的取值为%d", desc, key, shared.getInt(key, 0));
- }
- else if (value instanceof Float) // 如果配对值的类型为浮点数
- {
- desc = String.format("%s\n %s的取值为%f", desc, key, shared.getFloat(key, 0.0f));
- }
- else if (value instanceof Boolean) // 如果配对值的类型为布尔数
- {
- desc = String.format("%s\n %s的取值为%b", desc, key, shared.getBoolean(key, false));
- }
- else if (value instanceof Long) // 如果配对值的类型为长整型
- {
- desc = String.format("%s\n %s的取值为%d", desc, key, shared.getLong(key, 0L));
- }
- else // 如果配对值的类型为未知类型
- {
- desc = String.format("%s\n参数%s的取值为未知类型", desc, key);
- }
- }
-
-
- if (mapParam.size() <= 0)
- {
- desc = "共享参数中保存的信息为空";
- }
-
- tv_share.setText(desc);
- }
-
- }



ToastUtil
- package com.example.myapplication;
-
- import android.content.Context;
- import android.widget.Toast;
-
- public class ToastUtil
- {
- public static void show(Context ctx, String desc)
- {
- Toast.makeText(ctx, desc, Toast.LENGTH_SHORT).show();
- }
- }

DateUtil
- package com.example.myapplication;
-
- import android.annotation.SuppressLint;
- import android.text.TextUtils;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- @SuppressLint("SimpleDateFormat")
- public class DateUtil
- {
- // 获取当前的日期时间
- public static String getNowDateTime(String formatStr)
- {
- String format = formatStr;
-
- if (TextUtils.isEmpty(format))
- {
- format = "yyyyMMddHHmmss";
- }
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- return sdf.format(new Date());
- }
-
- // 获取当前的时间
- public static String getNowTime()
- {
- SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
- return sdf.format(new Date());
- }
-
- // 获取当前的时间(精确到毫秒)
- public static String getNowTimeDetail()
- {
- SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
- return sdf.format(new Date());
- }
-
- }

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

shape_edit_focus
- <shape xmlns:android="http://schemas.android.com/apk/res/android" >
-
-
- <solid android:color="#ffffff" />
-
-
- <stroke
- android:width="2dp"
- android:color="#DF162E" />
-
-
- <corners android:radius="5dp" />
-
-
- <padding
- android:bottom="2dp"
- android:left="2dp"
- android:right="2dp"
- android:top="2dp" />
-
- shape>

shape_edit_normal
- <shape xmlns:android="http://schemas.android.com/apk/res/android" >
-
-
- <solid android:color="#ffffff" />
-
-
- <stroke
- android:width="2dp"
- android:color="#aaaaaa" />
-
-
- <corners android:radius="5dp" />
-
-
- <padding
- android:bottom="2dp"
- android:left="2dp"
- android:right="2dp"
- android:top="2dp" />
-
- shape>









===========================================================================================================
布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="5dp" >
-
- <RadioGroup
- android:id="@+id/rg_login"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:orientation="horizontal" >
-
- <RadioButton
- android:id="@+id/rb_password"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:checked="true"
- android:gravity="left|center"
- android:text="密码登录"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <RadioButton
- android:id="@+id/rb_verifycode"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:checked="false"
- android:gravity="left|center"
- android:text="验证码登录"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RadioGroup>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="50dp" >
-
- <TextView
- android:id="@+id/tv_phone"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="手机号码:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <EditText
- android:id="@+id/et_phone"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="5dp"
- android:layout_marginTop="5dp"
- android:layout_toRightOf="@+id/tv_phone"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入手机号码"
- android:inputType="number"
- android:maxLength="11"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="50dp" >
-
- <TextView
- android:id="@+id/tv_password"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="登录密码:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_toRightOf="@+id/tv_password" >
-
- <EditText
- android:id="@+id/et_password"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="5dp"
- android:layout_marginTop="5dp"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入密码"
- android:inputType="numberPassword"
- android:maxLength="6"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <Button
- android:id="@+id/btn_forget"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_alignParentRight="true"
- android:gravity="center"
- android:text="忘记密码"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
- RelativeLayout>
-
- <CheckBox
- android:id="@+id/ck_remember"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:checked="false"
- android:padding="10dp"
- android:text="记住密码"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <Button
- android:id="@+id/btn_login"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="登 录"
- android:textColor="@color/black"
- android:textSize="20sp" />
-
- LinearLayout>

代码:
- package com.example.myapplication;
-
- import android.annotation.SuppressLint;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- import java.util.Random;
-
- @SuppressLint("DefaultLocale")
- public class LoginShareActivity extends AppCompatActivity implements View.OnClickListener
- {
- private RadioGroup rg_login; // 声明一个单选组对象
- private RadioButton rb_password; // 声明一个单选按钮对象
- private RadioButton rb_verifycode; // 声明一个单选按钮对象
- private EditText et_phone; // 声明一个编辑框对象
- private TextView tv_password; // 声明一个文本视图对象
- private EditText et_password; // 声明一个编辑框对象
- private Button btn_forget; // 声明一个按钮控件对象
- private CheckBox ck_remember; // 声明一个复选框对象
-
- private int mRequestCode = 0; // 跳转页面时的请求代码
- private boolean isRemember = false; // 是否记住密码
- private String mPassword = "111111"; // 默认密码
- private String mVerifyCode; // 验证码
-
- private SharedPreferences mShared; // 声明一个共享参数对象
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login_share);
-
-
- rg_login = findViewById(R.id.rg_login);
- rb_password = findViewById(R.id.rb_password);
- rb_verifycode = findViewById(R.id.rb_verifycode);
- et_phone = findViewById(R.id.et_phone);
- tv_password = findViewById(R.id.tv_password);
- et_password = findViewById(R.id.et_password);
- btn_forget = findViewById(R.id.btn_forget);
- ck_remember = findViewById(R.id.ck_remember);
-
- // 给rg_login设置单选监听器
- rg_login.setOnCheckedChangeListener(new RadioListener());
-
- // 给ck_remember设置勾选监听器
- ck_remember.setOnCheckedChangeListener(new CheckListener());
-
- // 给et_phone添加文本变更监听器
- et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11));
-
- // 给et_password添加文本变更监听器
- et_password.addTextChangedListener(new HideTextWatcher(et_password, 11));
-
- btn_forget.setOnClickListener(this);
-
- findViewById(R.id.btn_login).setOnClickListener(this);
-
- // 从share_login.xml获取共享参数对象
- mShared = getSharedPreferences("share_login", MODE_PRIVATE);
-
- // 获取共享参数保存的手机号码
- String phone = mShared.getString("phone", "");
-
- // 获取共享参数保存的密码
- String password = mShared.getString("password", "");
-
- et_phone.setText(phone); // 往手机号码编辑框填写上次保存的手机号
-
- et_password.setText(password); // 往密码编辑框填写上次保存的密码
- }
-
- // 定义登录方式的单选监听器
- private class RadioListener implements RadioGroup.OnCheckedChangeListener
- {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId)
- {
- if (checkedId == R.id.rb_password) // 选择了密码登录
- {
- tv_password.setText("登录密码:");
- et_password.setHint("请输入密码");
- btn_forget.setText("忘记密码");
- ck_remember.setVisibility(View.VISIBLE);
- }
- else if (checkedId == R.id.rb_verifycode) // 选择了验证码登录
- {
- tv_password.setText(" 验证码:");
- et_password.setHint("请输入验证码");
- btn_forget.setText("获取验证码");
- ck_remember.setVisibility(View.GONE);
- }
- }
- }
-
- // 定义是否记住密码的勾选监听器
- private class CheckListener implements CompoundButton.OnCheckedChangeListener
- {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
- {
- if (buttonView.getId() == R.id.ck_remember)
- {
- isRemember = isChecked;
- }
- }
- }
-
- // 定义一个编辑框监听器,在输入文本达到指定长度时自动隐藏输入法
- private class HideTextWatcher implements TextWatcher
- {
- private EditText mView; // 声明一个编辑框对象
- private int mMaxLength; // 声明一个最大长度变量
-
- public HideTextWatcher(EditText v, int maxLength)
- {
- super();
- mView = v;
- mMaxLength = maxLength;
- }
-
- // 在编辑框的输入文本变化前触发
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
-
- // 在编辑框的输入文本变化时触发
- public void onTextChanged(CharSequence s, int start, int before, int count) {}
-
- // 在编辑框的输入文本变化后触发
- public void afterTextChanged(Editable s)
- {
- String str = s.toString(); // 获得已输入的文本字符串
-
-
- // 输入文本达到11位(如手机号码),或者达到6位(如登录密码)时关闭输入法
- if ((str.length() == 11 && mMaxLength == 11)
- || (str.length() == 6 && mMaxLength == 6))
- {
- ViewUtil.hideOneInputMethod(LoginShareActivity.this, mView); // 隐藏输入法软键盘
- }
- }
- }
-
- @Override
- public void onClick(View v)
- {
- String phone = et_phone.getText().toString();
- if (v.getId() == R.id.btn_forget) { // 点击了“忘记密码”按钮
- if (phone.length() < 11) { // 手机号码不足11位
- Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
- return;
- }
- if (rb_password.isChecked()) { // 选择了密码方式校验,此时要跳到找回密码页面
- // 以下携带手机号码跳转到找回密码页面
- Intent intent = new Intent(this, LoginForgetActivity.class);
- intent.putExtra("phone", phone);
- startActivityForResult(intent, mRequestCode); // 携带意图返回上一个页面
- } else if (rb_verifycode.isChecked()) { // 选择了验证码方式校验,此时要生成六位随机数字验证码
- // 生成六位随机数字的验证码
- mVerifyCode = String.format("%06d", new Random().nextInt(999999));
- // 以下弹出提醒对话框,提示用户记住六位验证码数字
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("请记住验证码");
- builder.setMessage("手机号" + phone + ",本次验证码是" + mVerifyCode + ",请输入验证码");
- builder.setPositiveButton("好的", null);
- AlertDialog alert = builder.create();
- alert.show(); // 显示提醒对话框
- }
- } else if (v.getId() == R.id.btn_login) { // 点击了“登录”按钮
- if (phone.length() < 11) { // 手机号码不足11位
- Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
- return;
- }
- if (rb_password.isChecked()) { // 密码方式校验
- if (!et_password.getText().toString().equals(mPassword)) {
- Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
- } else { // 密码校验通过
- loginSuccess(); // 提示用户登录成功
- }
- } else if (rb_verifycode.isChecked()) { // 验证码方式校验
- if (!et_password.getText().toString().equals(mVerifyCode)) {
- Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
- } else { // 验证码校验通过
- loginSuccess(); // 提示用户登录成功
- }
- }
- }
- }
-
- // 从下一个页面携带参数返回当前页面时触发
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == mRequestCode && data != null) {
- // 用户密码已改为新密码,故更新密码变量
- mPassword = data.getStringExtra("new_password");
- }
- }
-
- // 从修改密码页面返回登录页面,要清空密码的输入框
- @Override
- protected void onRestart()
- {
- super.onRestart();
- et_password.setText("");
- }
-
- // 校验通过,登录成功
- private void loginSuccess()
- {
- String desc = String.format("您的手机号码是%s,恭喜你通过登录验证,点击“确定”按钮返回上个页面", et_phone.getText().toString());
-
- // 以下弹出提醒对话框,提示用户登录成功
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
-
- builder.setTitle("登录成功");
- builder.setMessage(desc);
-
- builder.setPositiveButton("确定返回", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- finish(); // 结束当前的活动页面
- }
- });
-
-
- builder.setNegativeButton("我再看看", null);
- AlertDialog alert = builder.create();
- alert.show(); // 显示提醒对话框
-
- // 如果勾选了“记住密码”,就把手机号码和密码都保存到共享参数中
- if (isRemember)
- {
- SharedPreferences.Editor editor = mShared.edit(); // 获得编辑器的对象
- editor.putString("phone", et_phone.getText().toString()); // 添加名叫phone的手机号码
- editor.putString("password", et_password.getText().toString()); // 添加名叫password的密码
-
- editor.commit(); // 提交编辑器中的修改
- }
- }
-
- }







第二个布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="5dp" >
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="50dp" >
-
- <TextView
- android:id="@+id/tv_password_first"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="输入新密码:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <EditText
- android:id="@+id/et_password_first"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="5dp"
- android:layout_marginTop="5dp"
- android:layout_toRightOf="@+id/tv_password_first"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入新密码"
- android:inputType="numberPassword"
- android:maxLength="11"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="50dp" >
-
- <TextView
- android:id="@+id/tv_password_second"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="确认新密码:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <EditText
- android:id="@+id/et_password_second"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="5dp"
- android:layout_marginTop="5dp"
- android:layout_toRightOf="@+id/tv_password_second"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请再次输入新密码"
- android:inputType="numberPassword"
- android:maxLength="11"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="50dp" >
-
- <TextView
- android:id="@+id/tv_verifycode"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text=" 验证码:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_toRightOf="@+id/tv_verifycode" >
-
- <EditText
- android:id="@+id/et_verifycode"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="5dp"
- android:layout_marginTop="5dp"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入验证码"
- android:inputType="numberPassword"
- android:maxLength="6"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <Button
- android:id="@+id/btn_verifycode"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_alignParentRight="true"
- android:gravity="center"
- android:text="获取验证码"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
- RelativeLayout>
-
- <Button
- android:id="@+id/btn_confirm"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="确 定"
- android:textColor="@color/black"
- android:textSize="20sp" />
-
- LinearLayout>

代码:
- package com.example.myapplication;
-
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- import java.util.Random;
-
- @SuppressLint("DefaultLocale")
- public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener
- {
- private EditText et_password_first; // 声明一个编辑框对象
- private EditText et_password_second; // 声明一个编辑框对象
- private EditText et_verifycode; // 声明一个编辑框对象
-
- private String mVerifyCode; // 验证码
- private String mPhone; // 手机号码
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login_forget);
-
-
- // 从布局文件中获取名叫et_password_first的编辑框
- et_password_first = findViewById(R.id.et_password_first);
-
- // 从布局文件中获取名叫et_password_second的编辑框
- et_password_second = findViewById(R.id.et_password_second);
-
- // 从布局文件中获取名叫et_verifycode的编辑框
- et_verifycode = findViewById(R.id.et_verifycode);
-
- findViewById(R.id.btn_verifycode).setOnClickListener(this);
- findViewById(R.id.btn_confirm).setOnClickListener(this);
-
- // 从上一个页面获取要修改密码的手机号码
- mPhone = getIntent().getStringExtra("phone");
- }
-
- @Override
- public void onClick(View v)
- {
- if (v.getId() == R.id.btn_verifycode) // 点击了“获取验证码”按钮
- {
-
- if (mPhone == null || mPhone.length() < 11)
- {
- Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
- return;
- }
- // 生成六位随机数字的验证码
- mVerifyCode = String.format("%06d", new Random().nextInt(999999));
- // 以下弹出提醒对话框,提示用户记住六位验证码数字
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
-
- builder.setTitle("请记住验证码");
- builder.setMessage("手机号" + mPhone + ",本次验证码是" + mVerifyCode + ",请输入验证码");
- builder.setPositiveButton("好的", null);
-
- AlertDialog alert = builder.create();
-
- alert.show(); // 显示提醒对话框
- }
- else if (v.getId() == R.id.btn_confirm) // 点击了“确定”按钮
- {
- String password_first = et_password_first.getText().toString();
- String password_second = et_password_second.getText().toString();
-
- if (password_first.length() < 6 || password_second.length() < 6)
- {
- Toast.makeText(this, "请输入正确的新密码", Toast.LENGTH_SHORT).show();
- return;
- }
-
- if (!password_first.equals(password_second))
- {
- Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
- return;
- }
-
- if (!et_verifycode.getText().toString().equals(mVerifyCode))
- {
- Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
- }
- else
- {
- Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
-
- // 以下把修改好的新密码返回给上一个页面
- Intent intent = new Intent(); // 创建一个新意图
- intent.putExtra("new_password", password_first); // 存入新密码
- setResult(Activity.RESULT_OK, intent); // 携带意图返回上一个页面
- finish(); // 结束当前的活动页面
- }
- }
- }
-
- }







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

私有存储空间与公共存储空间
Android把外部存储分成了两块区域,一块是所有应用均可访问的公共空间,另一块是只有应用自己才可访问的私有空间。
Android在SD卡的“Android/data”目录下给每个应用又单独建了一个文件目录,用于给应用保存自己需要处理的临时文件。
这个给每个应用单独建立的文件目录,只有当前应用才能够读写文件,其它应用是不允许进行读写的,故而“Android/data”目录算是外部存储上的私有空间。
Android从7.0开始加强了SD卡的权限管理,App使用SD卡的公共控件前既需要事先声明权限,又需要在设置页面开启权限,使用私有空间无需另外设置权限。

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

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




布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="5dp" >
-
- <TextView
- android:id="@+id/tv_path"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- LinearLayout>

代码:
- package com.example.myapplication;
-
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Environment;
- import android.widget.TextView;
- import androidx.appcompat.app.AppCompatActivity;
-
- public class FilePathActivity extends AppCompatActivity
- {
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_file_path);
-
-
- TextView tv_path = findViewById(R.id.tv_path);
-
-
- // Android7.0之后默认禁止访问公共存储目录
- // 获取系统的公共存储路径
- String publicPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
-
-
- // 获取当前App的私有存储路径
- String privatePath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();
-
- boolean isLegacy = true;
-
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
- {
- // Android10的存储空间默认采取分区方式,此处判断是传统方式还是分区方式
- isLegacy = Environment.isExternalStorageLegacy();
- }
-
- String desc = "系统的公共存储路径位于" + publicPath +
- "\n\n当前App的私有存储路径位于" + privatePath +
- "\n\nAndroid7.0之后默认禁止访问公共存储目录" +
- "\n\n当前App的存储空间采取" + (isLegacy?"传统方式":"分区方式");
-
- tv_path.setText(desc);
- }
-
- }



存储卡上读写文本文件
布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="5dp" >
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="40dp" >
-
- <TextView
- android:id="@+id/tv_name"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="姓名:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <EditText
- android:id="@+id/et_name"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="3dp"
- android:layout_marginTop="3dp"
- android:layout_toRightOf="@+id/tv_name"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入姓名"
- android:inputType="text"
- android:maxLength="12"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="40dp" >
-
- <TextView
- android:id="@+id/tv_age"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="年龄:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <EditText
- android:id="@+id/et_age"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="3dp"
- android:layout_marginTop="3dp"
- android:layout_toRightOf="@+id/tv_age"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入年龄"
- android:inputType="number"
- android:maxLength="2"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="40dp" >
-
- <TextView
- android:id="@+id/tv_height"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="身高:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <EditText
- android:id="@+id/et_height"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="3dp"
- android:layout_marginTop="3dp"
- android:layout_toRightOf="@+id/tv_height"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入身高"
- android:inputType="number"
- android:maxLength="3"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="40dp" >
-
- <TextView
- android:id="@+id/tv_weight"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="体重:"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <EditText
- android:id="@+id/et_weight"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="3dp"
- android:layout_marginTop="3dp"
- android:layout_toRightOf="@+id/tv_weight"
- android:background="@drawable/editext_selector"
- android:gravity="left|center"
- android:hint="请输入体重"
- android:inputType="numberDecimal"
- android:maxLength="5"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="40dp" >
-
- <CheckBox
- android:id="@+id/ck_married"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center"
- android:checked="false"
- android:text="已婚"
- android:textColor="@color/black"
- android:textSize="17sp" />
- RelativeLayout>
-
- <Button
- android:id="@+id/btn_save"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="保存文本到存储卡"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <TextView
- android:id="@+id/tv_path"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- LinearLayout>

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

shape_edit_focus.xml
- <shape xmlns:android="http://schemas.android.com/apk/res/android" >
-
-
- <solid android:color="#ffffff" />
-
-
- <stroke
- android:width="2dp"
- android:color="#DF162E" />
-
-
- <corners android:radius="5dp" />
-
-
- <padding
- android:bottom="2dp"
- android:left="2dp"
- android:right="2dp"
- android:top="2dp" />
-
- shape>

shape_edit_normal
- <shape xmlns:android="http://schemas.android.com/apk/res/android" >
-
-
- <solid android:color="#ffffff" />
-
-
- <stroke
- android:width="2dp"
- android:color="#aaaaaa" />
-
-
- <corners android:radius="5dp" />
-
-
- <padding
- android:bottom="2dp"
- android:left="2dp"
- android:right="2dp"
- android:top="2dp" />
-
- shape>

代码:
- package com.example.myapplication;
-
- import android.annotation.SuppressLint;
- import android.os.Bundle;
- import android.os.Environment;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.EditText;
- import android.widget.TextView;
- import androidx.appcompat.app.AppCompatActivity;
-
- @SuppressLint("SetTextI18n")
- public class FileWriteActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener
- {
- private EditText et_name;
- private EditText et_age;
- private EditText et_height;
- private EditText et_weight;
- private boolean isMarried = false;
- private String[] typeArray = {"未婚", "已婚"};
- private String mPath; // 私有目录路径
- private TextView tv_path;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_file_write);
-
-
- et_name = findViewById(R.id.et_name);
- et_age = findViewById(R.id.et_age);
- et_height = findViewById(R.id.et_height);
- et_weight = findViewById(R.id.et_weight);
- tv_path = findViewById(R.id.tv_path);
-
- CheckBox ck_married = findViewById(R.id.ck_married);
-
- ck_married.setOnCheckedChangeListener(this);
- findViewById(R.id.btn_save).setOnClickListener(this);
- // 获取当前App的私有下载目录
- mPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
- }
-
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
- {
- isMarried = isChecked;
- }
-
- @Override
- public void onClick(View v)
- {
- if (v.getId() == R.id.btn_save)
- {
- String name = et_name.getText().toString();
- String age = et_age.getText().toString();
- String height = et_height.getText().toString();
- String weight = et_weight.getText().toString();
-
- if (TextUtils.isEmpty(name))
- {
- ToastUtil.show(this, "请先填写姓名");
- return;
- }
- else if (TextUtils.isEmpty(age))
- {
- ToastUtil.show(this, "请先填写年龄");
- return;
- }
- else if (TextUtils.isEmpty(height))
- {
- ToastUtil.show(this, "请先填写身高");
- return;
- }
- else if (TextUtils.isEmpty(weight))
- {
- ToastUtil.show(this, "请先填写体重");
- return;
- }
-
- String content = String.format(" 姓名:%s\n 年龄:%s\n 身高:%scm\n 体重:%skg\n 婚否:%s\n 注册时间:%s\n",
- name, age, height, weight, typeArray[isMarried?1:0], DateUtil.getNowDateTime("yyyy-MM-dd HH:mm:ss"));
-
- String file_path = mPath + DateUtil.getNowDateTime("") + ".txt";
-
- FileUtil.saveText(file_path, content); // 把字符串内容保存为文本文件
-
- tv_path.setText("用户注册信息文件的保存路径为:\n" + file_path);
-
- ToastUtil.show(this, "数据已写入存储卡文件");
- }
- }
-
- }



第二个布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="5dp" >
-
- <Button
- android:id="@+id/btn_delete"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="删除所有文本文件"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <TextView
- android:id="@+id/tv_content"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- LinearLayout>

代码:
- package com.example.myapplication;
-
- import android.annotation.SuppressLint;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.View;
- import android.widget.TextView;
- import androidx.appcompat.app.AppCompatActivity;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
-
- @SuppressLint("SetTextI18n")
- public class FileReadActivity extends AppCompatActivity implements View.OnClickListener
- {
- private final static String TAG = "FileReadActivity";
- private TextView tv_content;
- private String mPath; // 私有目录路径
- private List
mFilelist = new ArrayList(); -
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_file_read);
-
-
- tv_content = findViewById(R.id.tv_content);
-
- findViewById(R.id.btn_delete).setOnClickListener(this);
-
- // 获取当前App的私有下载目录
- mPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
-
- showFileContent(); // 显示最新的文本文件内容
- }
-
- // 显示最新的文本文件内容
- private void showFileContent()
- {
- // 获得指定目录下面的所有文本文件
- mFilelist = FileUtil.getFileList(mPath, new String[]{".txt"});
-
- if (mFilelist.size() > 0)
- {
- // 打开并显示选中的文本文件内容
- String file_path = mFilelist.get(0).getAbsolutePath();
- String content = FileUtil.openText(file_path);
- String desc = String.format("找到最新的文本文件,路径为%s,内容如下:\n%s",
- file_path, content);
- tv_content.setText(desc);
- }
- else
- {
- tv_content.setText("私有目录下未找到任何文本文件");
- }
- }
-
- @Override
- public void onClick(View v)
- {
- if (v.getId() == R.id.btn_delete)
- {
- for (int i = 0; i < mFilelist.size(); i++)
- {
- String file_path = mFilelist.get(i).getAbsolutePath();
- File f = new File(file_path);
-
- if (!f.delete())
- {
- Log.d(TAG, "file_path=" + file_path + ", delete failed");
- }
- }
-
- ToastUtil.show(this, "已删除私有目录下的所有文本文件");
- }
- }
-
- }



FileUtil
- package com.example.myapplication;
-
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FilenameFilter;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Locale;
-
- public class FileUtil
- {
-
- // 把字符串保存到指定路径的文本文件
- public static void saveText(String path, String txt)
- {
- // 根据指定的文件路径构建文件输出流对象
- try (FileOutputStream fos = new FileOutputStream(path))
- {
- fos.write(txt.getBytes()); // 把字符串写入文件输出流
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
-
- // 从指定路径的文本文件中读取内容字符串
- public static String openText(String path)
- {
- String readStr = "";
- // 根据指定的文件路径构建文件输入流对象
- try (FileInputStream fis = new FileInputStream(path))
- {
- byte[] b = new byte[fis.available()];
- fis.read(b); // 从文件输入流读取字节数组
- readStr = new String(b); // 把字节数组转换为字符串
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
-
- return readStr; // 返回文本文件中的文本字符串
- }
-
- // 把位图数据保存到指定路径的图片文件
- public static void saveImage(String path, Bitmap bitmap)
- {
- // 根据指定的文件路径构建文件输出流对象
- try (FileOutputStream fos = new FileOutputStream(path))
- {
- // 把位图数据压缩到文件输出流中
- bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
-
- // 从指定路径的图片文件中读取位图数据
- public static Bitmap openImage(String path)
- {
- Bitmap bitmap = null; // 声明一个位图对象
- // 根据指定的文件路径构建文件输入流对象
- try (FileInputStream fis = new FileInputStream(path))
- {
- bitmap = BitmapFactory.decodeStream(fis); // 从文件输入流中解码位图数据
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
-
- return bitmap; // 返回图片文件中的位图数据
- }
-
- public static List
getFileList(String path, String[] extendArray) - {
- List
displayedContent = new ArrayList(); - File[] files = null;
- File directory = new File(path);
-
- if (extendArray != null && extendArray.length > 0)
- {
- FilenameFilter fileFilter = getTypeFilter(extendArray);
- files = directory.listFiles(fileFilter);
- }
- else
- {
- files = directory.listFiles();
- }
-
- if (files != null)
- {
- for (File f : files)
- {
- if (!f.isDirectory() && !f.isHidden())
- {
- displayedContent.add(f);
- }
- }
- }
- // 按照最后修改时间排序
- Collections.sort(displayedContent, new Comparator
() - {
- @Override
- public int compare(File o1, File o2)
- {
- return (o1.lastModified() > o2.lastModified()) ? -1 : 1;
- }
- });
-
- return displayedContent;
- }
-
- public static FilenameFilter getTypeFilter(String[] extendArray)
- {
- final ArrayList
fileExtensions = new ArrayList(); -
- for (int i = 0; i < extendArray.length; i++)
- {
- fileExtensions.add(extendArray[i]);
- }
-
- FilenameFilter fileNameFilter = new FilenameFilter()
- {
- @Override
- public boolean accept(File directory, String fileName)
- {
- boolean matched = false;
- File f = new File(String.format("%s/%s", directory.getAbsolutePath(), fileName));
-
- matched = f.isDirectory();
-
- if (!matched)
- {
- for (String s : fileExtensions)
- {
- s = String.format(".{0,}\\%s$", s);
-
- s = s.toUpperCase(Locale.getDefault());
-
- fileName = fileName.toUpperCase(Locale.getDefault());
-
- matched = fileName.matches(s);
-
- if (matched)
- {
- break;
- }
- }
- }
- return matched;
- }
- };
- return fileNameFilter;
- }
- }
ToastUtil
- package com.example.myapplication;
-
- import android.content.Context;
- import android.widget.Toast;
-
- public class ToastUtil
- {
- public static void show(Context ctx, String desc)
- {
- Toast.makeText(ctx, desc, Toast.LENGTH_SHORT).show();
- }
- }
ViewUtil
- package com.example.myapplication;
-
- import android.app.Activity;
- import android.content.Context;
- import android.view.View;
- import android.view.inputmethod.InputMethodManager;
-
- public class ViewUtil
- {
-
- public static void hideAllInputMethod(Activity act)
- {
- // 从系统服务中获取输入法管理器
- InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
-
- if (imm.isActive()) // 软键盘如果已经打开则关闭之
- {
- imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
- }
- }
-
- public static void hideOneInputMethod(Activity act, View v)
- {
- // 从系统服务中获取输入法管理器
- InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
-
- // 关闭屏幕上的输入法软键盘
- imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
- }
-
- }



==================================================================================================
在存储卡上读写图片
布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="5dp" >
-
- <ImageView
- android:id="@+id/iv_content"
- android:layout_width="match_parent"
- android:layout_height="250dp"
- android:scaleType="fitCenter" />
-
- <Button
- android:id="@+id/btn_save"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="把资源图片保存到存储卡"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <TextView
- android:id="@+id/tv_path"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- LinearLayout>

代码:
- package com.example.myapplication;
-
- import androidx.appcompat.app.AppCompatActivity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.os.Environment;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.TextView;
-
- public class ImageWriteActivity extends AppCompatActivity implements View.OnClickListener
- {
- private ImageView iv_content;
- private TextView tv_path;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_image_write);
-
-
- iv_content = findViewById(R.id.iv_content);
- iv_content.setImageResource(R.drawable.huawei); // 设置图像视图的图片资源
-
- tv_path = findViewById(R.id.tv_path);
-
- findViewById(R.id.btn_save).setOnClickListener(this);
- }
-
- @Override
- public void onClick(View v)
- {
- if (v.getId() == R.id.btn_save)
- {
- // 获取当前App的私有下载目录
- String path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
-
- // 从指定的资源文件中获取位图对象
- Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.huawei);
-
- String file_path = path + DateUtil.getNowDateTime("") + ".jpeg";
-
- FileUtil.saveImage(file_path, bitmap); // 把位图对象保存为图片文件
-
- tv_path.setText("图片文件的保存路径为:\n" + file_path);
-
- ToastUtil.show(this, "图片已写入存储卡文件");
- }
- }
-
- }


布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="5dp" >
-
- <Button
- android:id="@+id/btn_delete"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="删除所有图片文件"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <TextView
- android:id="@+id/tv_content"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textColor="@color/black"
- android:textSize="17sp" />
-
- <ImageView
- android:id="@+id/iv_content"
- android:layout_width="match_parent"
- android:layout_height="250dp"
- android:scaleType="fitCenter" />
-
- LinearLayout>

代码:
- package com.example.myapplication;
-
- import androidx.appcompat.app.AppCompatActivity;
- import android.graphics.Bitmap;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.TextView;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
-
- public class ImageReadActivity extends AppCompatActivity implements View.OnClickListener
- {
- private final static String TAG = "ImageReadActivity";
- private TextView tv_content;
- private ImageView iv_content;
- private String mPath; // 私有目录路径
- private List
mFilelist = new ArrayList(); -
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_image_read);
-
-
- tv_content = findViewById(R.id.tv_content);
- iv_content = findViewById(R.id.iv_content);
-
- findViewById(R.id.btn_delete).setOnClickListener(this);
-
- // 获取当前App的私有下载目录
- mPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
-
- showFileContent(); // 显示最新的图片文件内容
- }
-
- // 显示最新的图片文件内容
- private void showFileContent()
- {
- // 获得指定目录下面的所有图片文件
- mFilelist = FileUtil.getFileList(mPath, new String[]{".jpeg"});
-
- if (mFilelist.size() > 0)
- {
- // 打开并显示选中的图片文件内容
- String file_path = mFilelist.get(0).getAbsolutePath();
- tv_content.setText("找到最新的图片文件,路径为"+file_path);
-
- // 显示存储卡图片文件的第一种方式:直接调用setImageURI方法
- //iv_content.setImageURI(Uri.parse(file_path)); // 设置图像视图的路径对象
-
- // 第二种方式:先调用BitmapFactory.decodeFile获得位图,再调用setImageBitmap方法
- //Bitmap bitmap = BitmapFactory.decodeFile(file_path);
- //iv_content.setImageBitmap(bitmap); // 设置图像视图的位图对象
-
- // 第三种方式:先调用FileUtil.openImage获得位图,再调用setImageBitmap方法
- Bitmap bitmap = FileUtil.openImage(file_path);
- iv_content.setImageBitmap(bitmap); // 设置图像视图的位图对象
- }
- else
- {
- tv_content.setText("私有目录下未找到任何图片文件");
- }
- }
-
- @Override
- public void onClick(View v)
- {
- if (v.getId() == R.id.btn_delete)
- {
- for (int i = 0; i < mFilelist.size(); i++)
- {
- // 获取该文件的绝对路径字符串
- String file_path = mFilelist.get(i).getAbsolutePath();
- File f = new File(file_path);
-
- if (!f.delete()) // 删除文件,并判断是否成功删除
- {
- Log.d(TAG, "file_path=" + file_path + ", delete failed");
- }
- }
-
- ToastUtil.show(this, "已删除私有目录下的所有图片文件");
- }
- }
-
- }


















PS:
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.chapter06">
-
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
-
- <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
-
- <application
- android:name=".MainApplication"
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- intent-filter>
- activity>
- <activity android:name=".ShareWriteActivity" />
- <activity android:name=".ShareReadActivity" />
- <activity android:name=".LoginShareActivity" />
- <activity android:name=".LoginForgetActivity" />
- <activity android:name=".DatabaseActivity" />
- <activity android:name=".SQLiteWriteActivity" />
- <activity android:name=".SQLiteReadActivity" />
- <activity android:name=".LoginSQLiteActivity" />
- <activity android:name=".FilePathActivity" />
- <activity android:name=".FileWriteActivity" />
- <activity android:name=".FileReadActivity" />
- <activity android:name=".ImageWriteActivity" />
- <activity android:name=".ImageReadActivity" />
- <activity android:name=".ActTestActivity" />
- <activity android:name=".AppWriteActivity" />
- <activity android:name=".AppReadActivity" />
- <activity android:name=".RoomWriteActivity" />
- <activity android:name=".RoomReadActivity" />
- <activity
- android:name=".ShoppingCartActivity"
- android:theme="@style/AppCompatTheme" />
- <activity
- android:name=".ShoppingChannelActivity"
- android:theme="@style/AppCompatTheme" />
- <activity
- android:name=".ShoppingDetailActivity"
- android:theme="@style/AppCompatTheme" />
- application>
-
- manifest>
