简体   繁体   English

Android:回车键或制表键仍使用 jetpack compose 在密码输入字段中输入值

[英]Android: enter or tab keys still enters values into the password input field using jetpack compose

When I enter or tab keys still enters values into the password input field using jetpack compose.当我输入或制表键时,仍然使用 jetpack compose 在密码输入字段中输入值。

Below is my code snippet:下面是我的代码片段:

val (focusRequester) = FocusRequester.createRefs()

TextField(
    value = text,
    onValueChange = {
        text = it
    },
    singleLine = true,
    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
    keyboardActions = KeyboardActions(
        onDone = { focusRequester.requestFocus() }
    ),
    modifier = Modifier.onKeyEvent {
        if (it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_ENTER){
            focusRequester.requestFocus()
            true
        }
        false
    }
)

Tab key and enter key entries from laptop keyboard via Vysor not from android keyboard. Tab key并通过 Vysor 从笔记本电脑键盘enter key条目,而不是从 android 键盘。

Any suggestions are welcome here.欢迎在这里提出任何建议。 Thanks!谢谢!

Update 1:更新1:

Is there any way we can do for all the function keys like standards, with out using below code.我们有什么办法可以像标准一样为所有 function 键做任何事情,而不使用下面的代码。

modifier = Modifier.onKeyEvent {
        if (it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_ENTER || it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_TAB) {
           focusManager.moveFocus(FocusDirection.Next)
      }
        false
    }

You can use a regex, something like:您可以使用正则表达式,例如:

 val pattern = remember { Regex("^[^\\t\\n]*\$") }

 TextField(
        value = text,
        onValueChange = {
            if (it.isEmpty() || it.matches(pattern)) {
                text = it
            }
        },
        singleLine = true,
        maxLines= 1,
        //
   )

I think it should help you to clear focus after clicking on keyboard Done button.我认为它应该可以帮助您在单击键盘完成按钮后清除焦点。

val focusManager = LocalFocusManager.current

TextField(
    value = text,
    onValueChange = {
       text = it
    },
    singleLine = true,
    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
    keyboardActions = KeyboardActions(
       onDone = { focusManager.clearFocus() }
    ),
)
val (focusRequester) = FocusRequester.createRefs()

TextField(
    value = text,
    onValueChange = {
            if (it.isEmpty() || it.matches(pattern)) {
                text = it
            }
        },
    singleLine = true,
    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
    keyboardActions = KeyboardActions(
        onDone = { focusRequester.requestFocus() }
    ),
    modifier = Modifier.onKeyEvent {
        if (it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_ENTER || it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_TAB) {
           focusManager.moveFocus(FocusDirection.Next)
      }
        false
    }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM