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

huangapple go评论89阅读模式
英文:

How to enable capitalization on the virtual keyboard for an Android Compose TextField?

问题

我最近开始使用Compose来创建Android中的用户界面。到目前为止,我很喜欢它,但有时仍然难以找到正确的文档。如果这个问题很明显,我很抱歉!

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

以下是我的TextField代码。一些背景信息,如果有帮助的话:TextField位于一个包含TextStack中。我用它来在TextField为空时显示提示。Stack位于一个Column中,而Column又位于包装整个屏幕的VerticalScroller中。我使用的是Android Studio 4.0 Canary 7。

// 保存TextField当前状态的模型
val modelTitle = +state { EditorModel() }
// 获取焦点和输入法管理器所需的上下文(即Activity)
val context = +ambient(ContextAmbient)
// 输入法管理器,用于隐藏键盘
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 = {
        // 获取当前位于焦点的视图,或创建一个
        var view = (context as Activity).currentFocus
        if (view == null)
            view = View(context)

        // 使用视图来隐藏键盘
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }
)
英文:

I've recently started moving to Compose to make my UIs in Android. So far I like it, but I'm still struggling to find the right documentation sometimes. So I'm sorry if this question is very obvious!

In the app I'm currently working on, I have a TextField that is used to enter the title of a message. Everything works fine, except that the virtual (on-screen) keyboard doesn't by default enable caps lock for the first letter. Is it possible to enable caps lock on the virtual keyboard for a TextField, and if so, how? It's only necessary for the first character in the TextField (or in a sentence, it's a title field so there should only be one sentence), if a user wants to capitalize more they are welcome to do it themselves 如何在Android Compose TextField上启用虚拟键盘的大写? So what I'm basically looking for is the Compose version of the android:inputType="textCapSentences" XML-attribute of the EditText.

My code for the TextField is below. Some background in case that helps: the TextField is inside a Stack which also holds a Text. I use that to display a hint in case the TextField is empty. The Stack is inside a Column, which in it's turn is inside a VerticalScroller that wraps the whole screen. I'm using Android Studio 4.0 Canary 7.

Thanks very much in advance!

// 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)
    },
}

答案1

得分: 27

你可以使用 keyboardOptionsKeyboardCapitalization(我在 alpha09 版本上):

TextField(
    ...,
    keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences)
)
英文:

You can use keyboardOptionsand KeyboardCapitalization (FYI I'm on alpha09):

TextField(
    ...,
    keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences)
)

答案2

得分: 5

使用 1.0.0-beta02 版本,您可以使用 keyboardOptions 属性:

       TextField(
             keyboardOptions = KeyboardOptions.Default.copy(
                    capitalization = KeyboardCapitalization.Sentences)
       )
英文:

With the 1.0.0-beta02 you can use the keyboardOptions attribute:

   TextField(
         keyboardOptions = KeyboardOptions.Default.copy(
                capitalization = KeyboardCapitalization.Sentences)
   )

huangapple
  • 本文由 发表于 2020年1月4日 01:48:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定