优化:在输入框失去焦点时校验输入值 #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.content.Intent
import android.location.Criteria
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -273,31 +271,25 @@ class SettingsFragment : BaseFragment<FragmentTasksActionSettingsBinding?>(), Vi
}
}
//设置位置更新最小时间间隔(单位:毫秒); 默认间隔10000毫秒最小间隔1000毫秒
binding!!.etMinInterval.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable) {
val changedText = s.toString()
if (changedText.isEmpty() || changedText == "0") {
binding!!.etMinInterval.setOnFocusChangeListener { _, hasFocus ->
if (!hasFocus) {
val inputText = binding!!.etMinInterval.text.toString()
if (inputText.isEmpty() || inputText == "0") {
binding!!.etMinInterval.setText("1")
binding!!.etMinInterval.setSelection(binding!!.etMinInterval.text.length) // 将光标移至文本末尾
return
}
}
})
}
//设置位置更新最小距离单位默认距离0米
binding!!.etMinDistance.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable) {
val changedText = s.toString()
if (changedText.isEmpty()) {
binding!!.etMinDistance.setOnFocusChangeListener { _, hasFocus ->
if (!hasFocus) {
val inputText = binding!!.etMinDistance.text.toString()
if (inputText.isEmpty()) {
binding!!.etMinDistance.setText("0")
binding!!.etMinDistance.setSelection(binding!!.etMinInterval.text.length) // 将光标移至文本末尾
return
binding!!.etMinDistance.setSelection(binding!!.etMinDistance.text.length) // 将光标移至文本末尾
}
}
})
}
binding!!.sbEnableSmsCommand.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
if (isChecked) {

View File

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

View File

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