English 中文(简体)
在ModalBottom没有显示文本现场 Sheet
原标题:Jetpack Compose TextToolbar not showing for TextField in ModalBottomSheet
  • 时间:2023-12-19 08:18:03
  •  标签:
  • android

在喷气池中 在<代码>Surface上,我有<>Text Field,并显示<代码>TextToolbar(关于<编码>Paste的改动)。 如果我把同样的<代码>Text Field输入(material3)ModalBottom Sheet 我看不到这一工具障碍吗? 我怎么能为ModalBottomSheet展示工具吧? 是什么使工具无法躲藏?

I ve创设了一个minimal demo here,供其他人复制。 所有重要障碍均载于MainActative here

“entertext

“entergraph

问题回答

Jetpack Compose is still a relatively new framework, and as such, some features are not supported yet. As far as I know, the TextToolbar is not supported to be used inside a ModalBottomSheet. This is because the ModalBottomSheet takes up the entire screen, and the TextToolbar is not positioned correctly in this context.
If you need a toolbar for your bottom sheet, I think you have two options :
1. Use a trailing Past icon with textField(easy way that works fine)
2. Create a custom toolbar component that is specifically designed to be used in ModalBottomSheet

Implementing the second way could be a little complex. I tried to implement this method in general.
This is a summary of what my code does:
1. I made a new composable specifically for using inside modal (TextFieldWithCustomToolBar)
2. The new composable has a TextFild and a DropdownMenu which will act instead of native toolbar
3. We need to detect long press on TextFild, Jetpack Compose has modifiers for handling various gestures, including drag, click, and scroll, but it doesn t work directly on text fields. This is because text fields have their own built-in gesture-handling system that intercepts user interactions before they reach the pointer input modifier. So I did it manually by collecting information from interactionSource

@Composable
fun TextFieldWithCustomToolBar(
    supportingText: String,
    onPasteRequested: () -> Unit
) {

    var expanded by remember {
        mutableStateOf(false)
    }
    val text = remember { mutableStateOf("") }
    val interactionSource = remember { MutableInteractionSource() }
    val viewConfiguration = LocalViewConfiguration.current

    //wait for a long press action
    LaunchedEffect(interactionSource) {
        var isDown = false
        interactionSource.interactions.collect { interaction ->
            when (interaction) {
                is PressInteraction.Press -> {
                    isDown = true
                    CoroutineScope(Dispatchers.Main).launch {
                        delay(viewConfiguration.longPressTimeoutMillis)
                        if (isDown) expanded = true
                    }
                }

                is PressInteraction.Release -> {
                    isDown = false
                }
            }
        }
    }
    Box {
        TextField(
            interactionSource = interactionSource,
            value = text.value,
            onValueChange = { text.value = it },
            supportingText = { Text(text = supportingText) }
        )

        DropdownMenu(
            expanded = expanded,
            properties = PopupProperties(focusable = false),
            onDismissRequest = { expanded = false },
        ) {
            DropdownMenuItem({ Text(text = "Paste") }, onClick = {
                expanded = false
                onPasteRequested.invoke()
            })
        }
    }
}

? 如果烟 the是你面临的一个问题,请查看answer





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签