防止Android上的单个项目的多次点击

ndh0cuux  于 2024-01-04  发布在  Android
关注(0)|答案(2)|浏览(118)

我想把我的插播广告实现到主题改变的图标上,但有一个问题。当用户快速点击那个图标时,广告会出现多次,这会让用户体验很烦恼。因为没有人希望多个广告只为一个目的。如何让它成为一个单一的点击,当用户点击那个图标时,它会经过广告,用户不能再按那个图标

代码片段:

  1. var isDarkMode by remember { mutableStateOf(isSystemInDarkTheme) }
  2. IconToggleButton(
  3. checked = isDarkMode,
  4. onCheckedChange = { isChecked ->
  5. isDarkMode = isChecked
  6. if (activity != null) {
  7. showInterstialAd(activity, context)
  8. }
  9. changeThemeViewModel.enableDarkTheme(isDarkMode)
  10. },
  11. modifier = Modifier.pulsateClick()
  12. ) {
  13. Icon(
  14. painter = painterResource(R.drawable.icon),
  15. contentDescription = stringResource(R.string.lamp_icon),
  16. modifier = Modifier
  17. .size(45.dp)
  18. )
  19. }

字符串

bfhwhh0e

bfhwhh0e1#

试试这个

  1. var isDarkMode by remember { mutableStateOf(isSystemInDarkTheme) }
  2. var isButtonClickable by remember { mutableStateOf(true) }
  3. // Your click handling function
  4. fun onButtonClick() {
  5. if (isButtonClickable) {
  6. isButtonClickable = false
  7. showInterstitialAd(this, applicationContext)
  8. changeThemeViewModel.enableDarkTheme(isDarkMode)
  9. // Coroutine delay to make the button clickable again after a certain duration
  10. lifecycleScope.launch {
  11. delay(700) // Adjust the duration as needed (e.g., 2000 milliseconds)
  12. isButtonClickable = true
  13. }
  14. }
  15. }

字符串

展开查看全部
ncgqoxb0

ncgqoxb02#

你有没有尝试过这样的东西?(也许尝试应用一些延迟线,它是启用太多)

  1. val isDarkMode by remember { mutableStateOf(isSystemInDarkTheme) }
  2. val isButtonEnabled by remember { mutableStateOf(true) }
  3. IconToggleButton(
  4. checked = isDarkMode,
  5. onCheckedChange = { isChecked ->
  6. isButtonEnabled.value = false
  7. isDarkMode = isChecked
  8. if (activity != null) {
  9. showInterstialAd(activity, context)
  10. }
  11. changeThemeViewModel.enableDarkTheme(isDarkMode)
  12. isButtonEnabled.value = true
  13. },
  14. enabled = isButtonEnabled,
  15. ) {
  16. // content
  17. }

字符串

展开查看全部

相关问题