优化:在输入框失去焦点时校验输入值 #479

This commit is contained in:
pppscn 2024-06-01 20:42:52 +08:00
parent f7d2544ee9
commit d5f476bc32
3 changed files with 55 additions and 83 deletions

View File

@ -3,9 +3,7 @@ package com.idormy.sms.forwarder.fragment.action
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.location.Criteria import android.location.Criteria
import android.text.Editable
import android.text.TextUtils import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -273,31 +271,25 @@ class SettingsFragment : BaseFragment<FragmentTasksActionSettingsBinding?>(), Vi
} }
} }
//设置位置更新最小时间间隔(单位:毫秒); 默认间隔10000毫秒最小间隔1000毫秒 //设置位置更新最小时间间隔(单位:毫秒); 默认间隔10000毫秒最小间隔1000毫秒
binding!!.etMinInterval.addTextChangedListener(object : TextWatcher { binding!!.etMinInterval.setOnFocusChangeListener { _, hasFocus ->
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} if (!hasFocus) {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} val inputText = binding!!.etMinInterval.text.toString()
override fun afterTextChanged(s: Editable) { if (inputText.isEmpty() || inputText == "0") {
val changedText = s.toString()
if (changedText.isEmpty() || changedText == "0") {
binding!!.etMinInterval.setText("1") binding!!.etMinInterval.setText("1")
binding!!.etMinInterval.setSelection(binding!!.etMinInterval.text.length) // 将光标移至文本末尾 binding!!.etMinInterval.setSelection(binding!!.etMinInterval.text.length) // 将光标移至文本末尾
return
} }
} }
}) }
//设置位置更新最小距离单位默认距离0米 //设置位置更新最小距离单位默认距离0米
binding!!.etMinDistance.addTextChangedListener(object : TextWatcher { binding!!.etMinDistance.setOnFocusChangeListener { _, hasFocus ->
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} if (!hasFocus) {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} val inputText = binding!!.etMinDistance.text.toString()
override fun afterTextChanged(s: Editable) { if (inputText.isEmpty()) {
val changedText = s.toString()
if (changedText.isEmpty()) {
binding!!.etMinDistance.setText("0") binding!!.etMinDistance.setText("0")
binding!!.etMinDistance.setSelection(binding!!.etMinInterval.text.length) // 将光标移至文本末尾 binding!!.etMinDistance.setSelection(binding!!.etMinDistance.text.length) // 将光标移至文本末尾
return }
} }
} }
})
binding!!.sbEnableSmsCommand.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean -> binding!!.sbEnableSmsCommand.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
if (isChecked) { if (isChecked) {

View File

@ -2,8 +2,6 @@ package com.idormy.sms.forwarder.fragment.condition
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -99,13 +97,11 @@ class LeaveAddressFragment : BaseFragment<FragmentTasksConditionLeaveAddressBind
binding!!.btnDel.setOnClickListener(this) binding!!.btnDel.setOnClickListener(this)
binding!!.btnSave.setOnClickListener(this) binding!!.btnSave.setOnClickListener(this)
binding!!.btnCurrentCoordinates.setOnClickListener(this) binding!!.btnCurrentCoordinates.setOnClickListener(this)
binding!!.etLongitude.addTextChangedListener(object : TextWatcher { binding!!.etLongitude.setOnFocusChangeListener { _, hasFocus ->
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} if (!hasFocus) {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
try { try {
val changedText = s.toString() val inputText = binding!!.etLongitude.text.toString()
if (changedText.isEmpty()) { if (inputText.isEmpty()) {
binding!!.etLongitude.setText("0") binding!!.etLongitude.setText("0")
binding!!.etLongitude.setSelection(binding!!.etLongitude.text.length) // 将光标移至文本末尾 binding!!.etLongitude.setSelection(binding!!.etLongitude.text.length) // 将光标移至文本末尾
} else { } else {
@ -113,17 +109,15 @@ class LeaveAddressFragment : BaseFragment<FragmentTasksConditionLeaveAddressBind
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
Log.e(TAG, "afterTextChanged error:$e") Log.e(TAG, "etLongitude error:$e")
} }
} }
}) }
binding!!.etLatitude.addTextChangedListener(object : TextWatcher { binding!!.etLatitude.setOnFocusChangeListener { _, hasFocus ->
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} if (!hasFocus) {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
try { try {
val changedText = s.toString() val inputText = binding!!.etLatitude.text.toString()
if (changedText.isEmpty()) { if (inputText.isEmpty()) {
binding!!.etLatitude.setText("0") binding!!.etLatitude.setText("0")
binding!!.etLatitude.setSelection(binding!!.etLatitude.text.length) // 将光标移至文本末尾 binding!!.etLatitude.setSelection(binding!!.etLatitude.text.length) // 将光标移至文本末尾
} else { } else {
@ -131,17 +125,15 @@ class LeaveAddressFragment : BaseFragment<FragmentTasksConditionLeaveAddressBind
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
Log.e(TAG, "afterTextChanged error:$e") Log.e(TAG, "etLatitude error:$e")
} }
} }
}) }
binding!!.etDistance.addTextChangedListener(object : TextWatcher { binding!!.etDistance.setOnFocusChangeListener { _, hasFocus ->
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} if (!hasFocus) {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
try { try {
val changedText = s.toString() val inputText = binding!!.etDistance.text.toString()
if (changedText.isEmpty()) { if (inputText.isEmpty()) {
binding!!.etDistance.setText("1") binding!!.etDistance.setText("1")
binding!!.etDistance.setSelection(binding!!.etDistance.text.length) // 将光标移至文本末尾 binding!!.etDistance.setSelection(binding!!.etDistance.text.length) // 将光标移至文本末尾
} else { } else {
@ -149,22 +141,20 @@ class LeaveAddressFragment : BaseFragment<FragmentTasksConditionLeaveAddressBind
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
Log.e(TAG, "afterTextChanged error:$e") Log.e(TAG, "etDistance error:$e")
} }
} }
}) }
binding!!.etAddress.addTextChangedListener(object : TextWatcher { binding!!.etAddress.setOnFocusChangeListener { _, hasFocus ->
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} if (!hasFocus) {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
try { try {
checkSetting(true) checkSetting(true)
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
Log.e(TAG, "afterTextChanged error:$e") Log.e(TAG, "etAddress error:$e")
}
} }
} }
})
} }
@SingleClick @SingleClick

View File

@ -2,8 +2,6 @@ package com.idormy.sms.forwarder.fragment.condition
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -99,13 +97,11 @@ class ToAddressFragment : BaseFragment<FragmentTasksConditionToAddressBinding?>(
binding!!.btnDel.setOnClickListener(this) binding!!.btnDel.setOnClickListener(this)
binding!!.btnSave.setOnClickListener(this) binding!!.btnSave.setOnClickListener(this)
binding!!.btnCurrentCoordinates.setOnClickListener(this) binding!!.btnCurrentCoordinates.setOnClickListener(this)
binding!!.etLongitude.addTextChangedListener(object : TextWatcher { binding!!.etLongitude.setOnFocusChangeListener { _, hasFocus ->
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} if (!hasFocus) {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
try { try {
val changedText = s.toString() val inputText = binding!!.etLongitude.text.toString()
if (changedText.isEmpty()) { if (inputText.isEmpty()) {
binding!!.etLongitude.setText("0") binding!!.etLongitude.setText("0")
binding!!.etLongitude.setSelection(binding!!.etLongitude.text.length) // 将光标移至文本末尾 binding!!.etLongitude.setSelection(binding!!.etLongitude.text.length) // 将光标移至文本末尾
} else { } else {
@ -113,17 +109,15 @@ class ToAddressFragment : BaseFragment<FragmentTasksConditionToAddressBinding?>(
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
Log.e(TAG, "afterTextChanged error:$e") Log.e(TAG, "etLongitude error:$e")
} }
} }
}) }
binding!!.etLatitude.addTextChangedListener(object : TextWatcher { binding!!.etLatitude.setOnFocusChangeListener { _, hasFocus ->
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} if (!hasFocus) {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
try { try {
val changedText = s.toString() val inputText = binding!!.etLatitude.text.toString()
if (changedText.isEmpty()) { if (inputText.isEmpty()) {
binding!!.etLatitude.setText("0") binding!!.etLatitude.setText("0")
binding!!.etLatitude.setSelection(binding!!.etLatitude.text.length) // 将光标移至文本末尾 binding!!.etLatitude.setSelection(binding!!.etLatitude.text.length) // 将光标移至文本末尾
} else { } else {
@ -131,17 +125,15 @@ class ToAddressFragment : BaseFragment<FragmentTasksConditionToAddressBinding?>(
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
Log.e(TAG, "afterTextChanged error:$e") Log.e(TAG, "etLatitude error:$e")
} }
} }
}) }
binding!!.etDistance.addTextChangedListener(object : TextWatcher { binding!!.etDistance.setOnFocusChangeListener { _, hasFocus ->
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} if (!hasFocus) {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
try { try {
val changedText = s.toString() val inputText = binding!!.etDistance.text.toString()
if (changedText.isEmpty()) { if (inputText.isEmpty()) {
binding!!.etDistance.setText("1") binding!!.etDistance.setText("1")
binding!!.etDistance.setSelection(binding!!.etDistance.text.length) // 将光标移至文本末尾 binding!!.etDistance.setSelection(binding!!.etDistance.text.length) // 将光标移至文本末尾
} else { } else {
@ -149,22 +141,20 @@ class ToAddressFragment : BaseFragment<FragmentTasksConditionToAddressBinding?>(
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
Log.e(TAG, "afterTextChanged error:$e") Log.e(TAG, "etDistance error:$e")
} }
} }
}) }
binding!!.etAddress.addTextChangedListener(object : TextWatcher { binding!!.etAddress.setOnFocusChangeListener { _, hasFocus ->
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} if (!hasFocus) {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
try { try {
checkSetting(true) checkSetting(true)
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
Log.e(TAG, "afterTextChanged error:$e") Log.e(TAG, "etAddress error:$e")
}
} }
} }
})
} }
@SingleClick @SingleClick