简体   繁体   English

点击时未调用列可点击 lambda 块 - jetpack compose

[英]column clickable lambda block is not invoked on tap - jetpack compose

Column(
    Modifier
        .padding(0.dp).clickable {
            expanded = !expanded
        }) {
    OutlinedTextField(
        value = selectedText,
        readOnly = true,
        onValueChange = {
            selectedText = it
            onItemSelected(selectedText)
        },
        modifier = Modifier
            .fillMaxWidth()
            .onGloballyPositioned { coordinates ->
                //This value is used to assign to the DropDown the same width
                textfieldSize = coordinates.size.toSize()
            },
        colors = TextFieldDefaults.textFieldColors(
            focusedIndicatorColor = pearl,
            unfocusedIndicatorColor = ash,
            backgroundColor = white
        ),
        trailingIcon = {
            Icon(icon, "contentDescription",
                Modifier.clickable { expanded = !expanded })
        },
        shape = RoundedCornerShape(12.dp),
        placeholder = {
            Text(text = hint)
        }, maxLines = 1, singleLine = true

    )
 DropdownMenu(
  expanded = expanded,
    ...
 )
    

I am having a drop down menu in jetpack compose, i need to show the dropdown when i click on the column, but clickable lambda block of the column is not getting invoked on tap.我在 jetpack compose 中有一个下拉菜单,我需要在单击列时显示下拉菜单,但是在点击时不会调用列的可单击 lambda 块。 What could be the issue?可能是什么问题?

The OutlinedTextField "steals" the tap event. OutlinedTextField “窃取”了点击事件。 Instead of using a click event, you can use the interactionSource to handle the event of that TextField.您可以使用interactionSource 来处理该TextField 的事件,而不是使用单击事件。

Declare the interactionSource within the code where you want to catch the event and pass it to the OutlinedTextField.在代码中声明要捕获事件的交互源并将其传递给 OutlinedTextField。

var expanded by remember { mutableStateOf(false) }
val interactionSource = remember { MutableInteractionSource() }
val isPressed: Boolean by interactionSource.collectIsPressedAsState()

if (isPressed) {
    expanded = true
}

Column(
    Modifier
        .padding(0.dp)) {
    OutlinedTextField(
        value = selectedText,
        interactionSource = interactionSource,
        readOnly = true,
        onValueChange = {
            selectedText = it
            onItemSelected(selectedText)
        },
        modifier = Modifier
            .fillMaxWidth()
            .onGloballyPositioned { coordinates ->
                //This value is used to assign to the DropDown the same width
                textfieldSize = coordinates.size.toSize()
            },
        colors = TextFieldDefaults.textFieldColors(
            focusedIndicatorColor = pearl,
            unfocusedIndicatorColor = ash,
            backgroundColor = white
        ),
        trailingIcon = {
            Icon(icon, "contentDescription",
                Modifier.clickable { expanded = !expanded })
        },
        shape = RoundedCornerShape(12.dp),
        placeholder = {
            Text(text = hint)
        }, maxLines = 1, singleLine = true

    )
 DropdownMenu(
  expanded = expanded,
  onDismissRequest = { expanded = false }
    ...
 )

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

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