In this article, you will learn about compose for android

Anti-quick click

instructions

Anti-quick click the main use of Modifier operator to achieve, the code is as follows

code

@Composable
fun Modifier.avoidRepeatclickable(millis: Long = 500, onClick: () -> Unit): Modifier {
    var timeStamp by remember {
        mutableStateOf(0L)}return clickable {
        if (System.currentTimeMillis() - timeStamp > millis) {
            onClick()
            timeStamp = System.currentTimeMillis()
        }
    }
}
Copy the code

Git:github.com/ananananzhu…

The effect

There are two buttons in the GIF,

  • The first one uses a normal clickable that changes color every time a tragedy is clicked;

  • The second button uses our anti-quick click operator. After multiple clicks within 500ms, the background changes color once.

Compose jumps to the Activity and gets the return result

instructions

You need to use the ActivityResult Api, which Is specifically extended by Compose

The required extension libraries are as follows:

androidx.activity:activity-compose:1.3. 0
Copy the code

code

val launcher = rememberLauncherForActivityResult(contract =object : ActivityResultContract<String, String>() {
        override fun parseResult(resultCode: Int, intent: Intent?).: String {
           returnintent? .getStringExtra("data")? :""
        }

        / * * *@paramActivityResultContract<String, String> is the first generic */ from ActivityResultContract<String, String>
        override fun createIntent(context: Context, input: String?).: Intent {
            return Intent(context,GenerateActivityResultActivity::class.java).apply {
                putExtra("data",input)
            }
        }
    } , onResult = {result-> // Result is ActivityResultContract
      
        the second generic
      ,>
        activityResult = result
    })
Copy the code

Git address: github.com/ananananzhu…

The effect