This article mainly explains some practical compose knowledge in Android, long-term update
Anti-quick click
illustrate
Anti-quick click is mainly implemented using the Modifier operator, 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()
}
}
}
复制代码
Effect
There are two buttons in the gif,
-
The first one uses a normal clickable, and the tragedy changes color every time it is clicked;
-
The second button uses our anti-quick click operator, and the background changes once after multiple clicks within 500ms;
Jump to Activity in Compose and get the return result
illustrate
Here you need to use ActivityResult Api, compose is specially extended
The extension libraries that need to be used are as follows:
androidx.activity:activity-compose:1.3.0
复制代码
code
val launcher = rememberLauncherForActivityResult(contract =object : ActivityResultContract<String, String>() {
override fun parseResult(resultCode: Int, intent: Intent?): String {
return intent?.getStringExtra("data")?:""
}
/**
* @param compose向Compose中传的数据 ActivityResultContract<String, String>的第一个泛型
*/
override fun createIntent(context: Context, input: String?): Intent {
return Intent(context,GenerateActivityResultActivity::class.java).apply {
putExtra("data",input)
}
}
} , onResult = {result-> //result 是ActivityResultContract<String, String>第二个泛型
activityResult = result
})
复制代码
git address: github.com/ananananzhu…