Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.4K
Views
Botón Long Press Listener en Android jetpack componer

Tengo una interfaz de usuario componible de Android con un Button .

¿Cómo puedo realizar un seguimiento de los eventos de pulsación prolongada de un botón? Lo tengo funcionando para la pulsación larga de Text , pero para el Button , no funciona. De la misma manera que a continuación, si aplico un modificador al botón, no funciona.

 Text( text = view.text, fontSize = view.textFontSize.toInt().sp, fontWeight = FontWeight(view.textFontWeight.toInt()), color = Color(android.graphics.Color.parseColor(view.textColor)), modifier = Modifier.clickable( onClick = { println("Single Click") }, onLongClick = { println("Long Click") }, onDoubleClick = { println("Double Tap") }, ), )
over 4 years ago · Hanz Gallego
4 answers
Answer question

0

La mejor manera de manejar esto es hacer rodar su propio Button . El Button Material es básicamente solo una Surface y una Row . La razón por la que agregar su propio Modifier.clickable no funciona es porque uno ya está configurado.

Entonces, si desea agregar onLongPress , etc., puede copiar/pegar la implementación predeterminada y pasar esos lambdas.

 @Composable @OptIn(ExperimentalMaterialApi::class, ExperimentalFoundationApi::class) fun Button( onClick: () -> Unit, modifier: Modifier = Modifier, onLongClick: (() -> Unit)? = null, onDoubleClick: (() -> Unit)? = null, enabled: Boolean = true, interactionState: InteractionState = remember { InteractionState() }, elevation: ButtonElevation? = ButtonDefaults.elevation(), shape: Shape = MaterialTheme.shapes.small, border: BorderStroke? = null, colors: ButtonColors = ButtonDefaults.buttonColors(), contentPadding: PaddingValues = ButtonDefaults.ContentPadding, content: @Composable RowScope.() -> Unit ) { val contentColor by colors.contentColor(enabled) Surface( shape = shape, color = colors.backgroundColor(enabled).value, contentColor = contentColor.copy(alpha = 1f), border = border, elevation = elevation?.elevation(enabled, interactionState)?.value ?: 0.dp, modifier = modifier.combinedClickable( onClick = onClick, onDoubleClick = onDoubleClick, onLongClick = onLongClick, enabled = enabled, role = Role.Button, interactionState = interactionState, indication = null ) ) { Providers(LocalContentAlpha provides contentColor.alpha) { ProvideTextStyle( value = MaterialTheme.typography.button ) { Row( Modifier .defaultMinSizeConstraints( minWidth = ButtonDefaults.MinWidth, minHeight = ButtonDefaults.MinHeight ) .indication(interactionState, rememberRipple()) .padding(contentPadding), horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically, content = content ) } } } }

Uso:

 Button( onClick = {}, onLongClick = {}, onDoubleClick = {} ) { Text(text = "I'm a button") }
over 4 years ago · Hanz Gallego Report

0

También se puede usar https://developer.android.com/jetpack/compose/gestures .

por ejemplo:

 import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.foundation.gestures.detectTapGestures modifier = Modifier .weight(2f) .pointerInput(Unit){ detectTapGestures( onLongPress = { // perform some action here.. } ) }
over 4 years ago · Hanz Gallego Report

0

Puede usar combinedClickable como el siguiente:

 Modifier .combinedClickable( onClick = { }, onLongClick = { }, )

Advertencia: con Compose 1.0.1 , este método está marcado como @ExperimentalFoundationApi , por lo que esta respuesta puede quedar obsoleta en futuras versiones.

over 4 years ago · Hanz Gallego Report

0

Según la documentación

 Modifier.pointerInput(Unit) { detectTapGestures( onPress = { /* Called when the gesture starts */ }, onDoubleTap = { /* Called on Double Tap */ }, onLongPress = { /* Called on Long Press */ }, onTap = { /* Called on Tap */ } ) }
over 4 years ago · Hanz Gallego Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!