如何在 Android Compose TextField 的虚拟键盘上启用大写?

rai*_*d89 15 android kotlin android-jetpack-compose android-compose-textfield

我最近开始转向使用 Compose 在 Android 中制作 UI。到目前为止我很喜欢它,但有时我仍然很难找到正确的文档。如果这个问题非常明显,我很抱歉!

在我当前正在开发的应用程序中,我有一个TextField用于输入消息标题的应用程序。一切工作正常,除了虚拟(屏幕上)键盘默认情况下不会启用第一个字母的大写锁定。是否可以在虚拟键盘上启用大写锁定TextField,如果可以,如何启用?仅对于TextField(或在一个句子中,它是一个标题字段,因此应该只有一个句子)中的第一个字符是必要的,如果用户想要大写更多,欢迎他们自己做:)所以我基本上是寻找的是android:inputType="textCapSentences"EditText 的 XML 属性的 Compose 版本。

我的代码TextField如下。一些背景知识以防万一: isTextField位于 a 内部Stack,也包含 a Text。我用它来显示提示,以防万一它TextField是空的。Stack位于 a 内,Column而 a 又位于VerticalScroller包裹整个屏幕的 a 内。我正在使用 Android Studio 4.0 Canary 7。

首先十分感谢!

// Model saving the current state of the TextField
val modelTitle = +state{EditorModel()}
// Context (aka the Activity) necessary to get the focus and input method manager
val context = +ambient(ContextAmbient)
// Input Method Manager, to hide the keyboard
val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager

TextField {
    value = modelTitle.value,
    modifier = ExpandedWidth.wraps(Spacing(5.dp)),
    textStyle = TextStyle(
        color = Color.White,
        fontSize = 30.sp
    ),
    onValueChange = {
        modelTitle.value = it
    },
    keyboardType = KeyboardType.Text,
    imeAction = ImeAction.Done,
    onImeActionPerformed = {
        // Get the view currently in focus, or make one
        var view = (context as Activity).currentFocus
        if(view == null)
            view = View(context)

        // Use the view to hide the keyboard
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    },
}
Run Code Online (Sandbox Code Playgroud)

Cyr*_*ind 29

你可以使用keyboardOptionsand KeyboardCapitalization(仅供参考,我在 alpha09):

TextField(
    ...,
    keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences)
)
Run Code Online (Sandbox Code Playgroud)


Gab*_*tti 6

通过 ,1.0.0-beta02您可以使用该keyboardOptions属性:

   TextField(
         keyboardOptions = KeyboardOptions.Default.copy(
                capitalization = KeyboardCapitalization.Sentences)
   )
Run Code Online (Sandbox Code Playgroud)