mirror of
https://github.com/karasevm/PrivateDNSAndroid.git
synced 2025-06-28 20:29:56 +00:00
Add Text field Validation (#9)
* Add Text field validation * Implement Better Validation
This commit is contained in:
parent
9aabfa6261
commit
85d6a7410c
3 changed files with 32 additions and 3 deletions
|
@ -43,6 +43,7 @@ dependencies {
|
|||
implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||
implementation 'com.google.android.material:material:1.11.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||
implementation 'com.google.guava:guava:33.1.0-android'
|
||||
|
||||
def shizuku_version = '13.1.5'
|
||||
implementation "dev.rikka.shizuku:api:$shizuku_version"
|
||||
|
|
|
@ -2,9 +2,15 @@ package ru.karasevm.privatednstoggle
|
|||
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.os.Bundle
|
||||
import android.text.Editable
|
||||
import android.text.TextUtils
|
||||
import android.text.TextWatcher
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import com.google.common.net.InternetDomainName
|
||||
import ru.karasevm.privatednstoggle.databinding.DialogAddBinding
|
||||
|
||||
|
||||
|
@ -60,7 +66,7 @@ class AddServerDialogFragment : DialogFragment() {
|
|||
) { _, _ ->
|
||||
listener.onDialogPositiveClick(
|
||||
this,
|
||||
binding.editTextServerAddr.text.toString()
|
||||
binding.editTextServerAddr.text.toString().trim()
|
||||
)
|
||||
}
|
||||
.setNegativeButton(R.string.cancel
|
||||
|
@ -71,5 +77,29 @@ class AddServerDialogFragment : DialogFragment() {
|
|||
} ?: throw IllegalStateException("Activity cannot be null")
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
|
||||
val button = ((dialog) as AlertDialog).getButton(DialogInterface.BUTTON_POSITIVE)
|
||||
binding.editTextServerAddr.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 server = binding.editTextServerAddr.text.toString().trim()
|
||||
if (TextUtils.isEmpty(server) || !isValidServer(server)) {
|
||||
button.isEnabled = false
|
||||
} else {
|
||||
binding.editTextServerAddr.error = null
|
||||
button.isEnabled = true
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun isValidServer(str: String): Boolean {
|
||||
return InternetDomainName.isValid(str)
|
||||
}
|
||||
|
||||
}
|
|
@ -5,8 +5,6 @@
|
|||
android:layout_height="wrap_content">
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="16dp"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue