You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
4.5 KiB
165 lines
4.5 KiB
package com.gyf.lib.uikit
|
|
|
|
import androidx.compose.foundation.text.KeyboardActions
|
|
import androidx.compose.foundation.text.KeyboardOptions
|
|
import androidx.compose.material.LocalTextStyle
|
|
import androidx.compose.material.OutlinedTextField
|
|
import androidx.compose.material.Text
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.livedata.observeAsState
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.platform.LocalFocusManager
|
|
import androidx.compose.ui.text.TextStyle
|
|
import androidx.compose.ui.text.input.ImeAction
|
|
import androidx.compose.ui.text.input.VisualTransformation
|
|
import androidx.lifecycle.LiveData
|
|
import androidx.lifecycle.MutableLiveData
|
|
import com.orhanobut.logger.Logger
|
|
|
|
|
|
enum class FormStatus {
|
|
//空
|
|
Empty,
|
|
|
|
//格式不匹配
|
|
FormatError,
|
|
|
|
//学号已存在
|
|
Repeat,
|
|
|
|
//校验通过
|
|
Valid
|
|
}
|
|
|
|
|
|
abstract class FormName<T>(val formDesc: String) {
|
|
protected val _formValue = MutableLiveData<T>()
|
|
val formValue: LiveData<T> = _formValue
|
|
open val formPlaceholder = "请输入$formDesc"
|
|
|
|
abstract fun onChange(value: T)
|
|
}
|
|
|
|
/**
|
|
* 文本输入框控制
|
|
*
|
|
* @property textLength
|
|
* @constructor
|
|
*
|
|
* @param formDesc
|
|
*/
|
|
open class StringForm(formDesc: String, val textLength: Int) :
|
|
FormName<String>(formDesc = formDesc) {
|
|
|
|
constructor(formDesc: String, textLength: Int, value: String) : this(
|
|
formDesc = formDesc,
|
|
textLength = textLength
|
|
) {
|
|
_formValue.value = value
|
|
}
|
|
|
|
override fun onChange(value: String) {
|
|
if (value.length <= textLength) {
|
|
_formValue.value = value
|
|
}
|
|
Logger.i("${formDesc}更新值:${_formValue.value}")
|
|
}
|
|
|
|
fun clean() {
|
|
_formValue.postValue("")
|
|
}
|
|
}
|
|
|
|
open class ValidStringForm(formDesc: String, textLength: Int) : StringForm(formDesc, textLength) {
|
|
protected val _statusForm = MutableLiveData(FormStatus.Empty)
|
|
val statusForm: LiveData<FormStatus> = _statusForm
|
|
|
|
override fun onChange(value: String) {
|
|
super.onChange(value)
|
|
when (_formValue.value?.length) {
|
|
null, 0 -> _statusForm.value = FormStatus.Empty
|
|
else -> check()
|
|
}
|
|
}
|
|
|
|
protected open fun check() {
|
|
_statusForm.value = FormStatus.Valid
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 通用文本输入框
|
|
*
|
|
* @param T
|
|
* @param modifier
|
|
* @param form
|
|
* @param singeLine
|
|
*/
|
|
@Composable
|
|
fun <T : StringForm> BaseTextField(
|
|
modifier: Modifier = Modifier,
|
|
form: T,
|
|
singeLine: Boolean = false,
|
|
keyboardOptions: KeyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done),
|
|
isError: Boolean = false,
|
|
visualTransformation: VisualTransformation = VisualTransformation.None,
|
|
readOnly: Boolean = false,
|
|
textStyle: TextStyle = LocalTextStyle.current,
|
|
leadingIcon: @Composable (() -> Unit)? = null,
|
|
trailingIcon: @Composable (() -> Unit)? = null,
|
|
) {
|
|
val name: String by form.formValue.observeAsState("")
|
|
val focusManager = LocalFocusManager.current
|
|
OutlinedTextField(
|
|
modifier = modifier,
|
|
value = name,
|
|
onValueChange = { form.onChange(it) },
|
|
label = { Text(text = form.formDesc) },
|
|
placeholder = { Text(text = form.formPlaceholder) },
|
|
singleLine = singeLine,
|
|
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
|
keyboardOptions = keyboardOptions,
|
|
leadingIcon = leadingIcon,
|
|
trailingIcon = trailingIcon,
|
|
isError = isError,
|
|
visualTransformation = visualTransformation,
|
|
readOnly = readOnly,
|
|
textStyle = textStyle
|
|
)
|
|
}
|
|
|
|
/**
|
|
* 带字符长度计数的文本编辑框
|
|
*
|
|
* @param T 字段类型
|
|
* @param modifier
|
|
* @param form 字段
|
|
* @param singeLine
|
|
* @param keyboardOptions
|
|
* @param isError
|
|
* @param visualTransformation
|
|
*/
|
|
@Composable
|
|
fun <T : StringForm> BaseTextField(
|
|
modifier: Modifier = Modifier,
|
|
form: T,
|
|
singeLine: Boolean = false,
|
|
keyboardOptions: KeyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done),
|
|
isError: Boolean = false,
|
|
visualTransformation: VisualTransformation = VisualTransformation.None,
|
|
readOnly: Boolean = false
|
|
) {
|
|
val name: String by form.formValue.observeAsState("")
|
|
BaseTextField(
|
|
modifier,
|
|
form,
|
|
singeLine,
|
|
keyboardOptions,
|
|
isError,
|
|
visualTransformation,
|
|
trailingIcon = { Text(text = "${name.length}/${form.textLength}") },
|
|
readOnly = readOnly
|
|
)
|
|
}
|
|
|
|
|