Skip to content

Commit

Permalink
refactor(welcome):refactor setting code
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCShou committed Oct 15, 2022
1 parent e521d6e commit 2ac7286
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
30 changes: 13 additions & 17 deletions app/src/main/java/com/zcshou/gogogo/FragmentSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,21 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
ListPreference pfJoystick = findPreference("setting_joystick_type");
if (pfJoystick != null) {
// 使用自定义 SummaryProvider
pfJoystick.setSummaryProvider((Preference.SummaryProvider<ListPreference>) preference -> "当前类型: " + Objects.requireNonNull(preference.getEntry()));
pfJoystick.setSummaryProvider((Preference.SummaryProvider<ListPreference>) preference -> getResources().getString(R.string.setting_current_value) + Objects.requireNonNull(preference.getEntry()));
pfJoystick.setOnPreferenceChangeListener((preference, newValue) -> newValue.toString().trim().length() != 0);
}

EditTextPreference pfWalk = findPreference("setting_walk");
if (pfWalk != null) {
// 使用自定义 SummaryProvider
pfWalk.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> "当前值: " + preference.getText());
pfWalk.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> getResources().getString(R.string.setting_current_value) + preference.getText());
pfWalk.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
Selection.setSelection(editText.getText(), editText.length());
});
pfWalk.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.toString().trim().length() == 0)
{
GoUtils.DisplayToast(this.getContext(),"输入无效");
if (newValue.toString().trim().length() == 0) {
GoUtils.DisplayToast(this.getContext(),getResources().getString(R.string.app_error_input));
return false;
}
return true;
Expand All @@ -57,31 +56,29 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

EditTextPreference pfRun = findPreference("setting_run");
if (pfRun != null) {
pfRun.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> "当前值: " + preference.getText());
pfRun.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> getResources().getString(R.string.setting_current_value) + preference.getText());
pfRun.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
Selection.setSelection(editText.getText(), editText.length());
});
pfRun.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.toString().trim().length() == 0)
{
GoUtils.DisplayToast(this.getContext(),"输入无效");
if (newValue.toString().trim().length() == 0) {
GoUtils.DisplayToast(this.getContext(),getResources().getString(R.string.app_error_input));
return false;
}
return true;
});
}
EditTextPreference pfBike = findPreference("setting_bike");
if (pfBike != null) {
pfBike.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> "当前值: " + preference.getText());
pfBike.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> getResources().getString(R.string.setting_current_value) + preference.getText());
pfBike.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
Selection.setSelection(editText.getText(), editText.length());
});
pfBike.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.toString().trim().length() == 0)
{
GoUtils.DisplayToast(this.getContext(),"输入无效");
if (newValue.toString().trim().length() == 0) {
GoUtils.DisplayToast(this.getContext(),getResources().getString(R.string.app_error_input));
return false;
}
return true;
Expand Down Expand Up @@ -109,15 +106,14 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
EditTextPreference pfPosHisValid = findPreference("setting_pos_history");
if (pfPosHisValid != null) {
// 使用自定义 SummaryProvider
pfPosHisValid.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> "当前值: " + preference.getText());
pfPosHisValid.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> getResources().getString(R.string.setting_current_value) + preference.getText());
pfPosHisValid.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
Selection.setSelection(editText.getText(), editText.length());
});
pfPosHisValid.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.toString().trim().length() == 0)
{
GoUtils.DisplayToast(this.getContext(),"输入无效");
if (newValue.toString().trim().length() == 0) {
GoUtils.DisplayToast(this.getContext(),getResources().getString(R.string.app_error_input));
return false;
}
return true;
Expand Down
46 changes: 24 additions & 22 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,35 @@
<string name="app_error_gps">定位不可用,请检查 GPS 是否开启</string>
<string name="app_error_network">网络不可用,请检查网络连接</string>
<string name="app_error_permission">权限不足,请授予相关权限</string>
<string name="app_error_input">输入有误,请检查输入是否合法</string>

<!-- 欢迎页 -->
<string name="welcome_btn_txt">进入应用</string>

<!-- 设置界面 -->
<string name="setting_group_move">移动</string>
<string name="setting_current_value">当前值: </string>
<string name="setting_joystick">摇杆类型</string>
<string name="setting_joystick_tips">重启摇杆后生效</string>
<string name="setting_walk">步行速度(米/秒)</string>
<string name="setting_walk_default">1.2</string>
<string name="setting_run">跑步速度(米/秒)</string>
<string name="setting_run_default">3.6</string>
<string name="setting_bike">驾驶速度(米/秒)</string>
<string name="setting_bike_default">10.0</string>
<string name="setting_group_log">记录</string>
<string name="setting_log_off">关闭日志</string>
<string name="setting_pos_history">历史记录有效期(天)</string>
<string name="setting_pos_history_default">7</string>
<string name="setting_group_about">关于</string>
<string name="setting_version">版本号</string>
<string name="setting_author">开发者</string>
<string name="setting_group_sys">系统</string>

<!-- 通知栏 -->
<string name="note_show">显示摇杆</string>
<string name="note_hide">隐藏摇杆</string>

<!-- 欢迎页 -->
<string name="welcome_btn_txt">进入应用</string>

<!-- 主界面 -->
<string name="label_longitude">经度</string>
<string name="label_latitude">纬度</string>
Expand Down Expand Up @@ -63,25 +84,6 @@
<string name="nav_menu_feedback">问题反馈</string>
<string name="nav_menu_contact">联系作者</string>

<!-- 设置界面 -->
<string name="setting_group_move">移动</string>
<string name="setting_joystick">摇杆类型</string>
<string name="setting_joystick_tips">重启摇杆后生效</string>
<string name="setting_walk">步行速度(米/秒)</string>
<string name="setting_walk_default">1.2</string>
<string name="setting_run">跑步速度(米/秒)</string>
<string name="setting_run_default">3.6</string>
<string name="setting_bike">驾驶速度(米/秒)</string>
<string name="setting_bike_default">10.0</string>
<string name="setting_group_log">记录</string>
<string name="setting_log_off">关闭日志</string>
<string name="setting_pos_history">历史记录有效期(天)</string>
<string name="setting_pos_history_default">7</string>
<string name="setting_group_about">关于</string>
<string name="setting_version">版本号</string>
<string name="setting_author">开发者</string>
<string name="setting_group_sys">系统</string>

<!-- 百度地图 -->
<string name="map_pic_sate">卫星图</string>
<string name="map_pic_normal">普通图</string>
Expand Down

0 comments on commit 2ac7286

Please sign in to comment.