Android Jetpack Compose - enableEdgeToEdge 函数、MaterialTheme 函数、remember 函数

enableEdgeToEdge 函数

kotlin 复制代码
@JvmName("enable")
@JvmOverloads
fun ComponentActivity.enableEdgeToEdge(
    statusBarStyle: SystemBarStyle = SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT),
    navigationBarStyle: SystemBarStyle = SystemBarStyle.auto(DefaultLightScrim, DefaultDarkScrim)
) {
    val view = window.decorView
    val statusBarIsDark = statusBarStyle.detectDarkMode(view.resources)
    val navigationBarIsDark = navigationBarStyle.detectDarkMode(view.resources)
    val impl = Impl ?: if (Build.VERSION.SDK_INT >= 29) {
        EdgeToEdgeApi29()
    } else if (Build.VERSION.SDK_INT >= 26) {
        EdgeToEdgeApi26()
    } else if (Build.VERSION.SDK_INT >= 23) {
        EdgeToEdgeApi23()
    } else if (Build.VERSION.SDK_INT >= 21) {
        EdgeToEdgeApi21()
    } else {
        EdgeToEdgeBase()
    }.also { Impl = it }
    impl.setUp(
        statusBarStyle, navigationBarStyle, window, view, statusBarIsDark, navigationBarIsDark
    )
}
  • 在调用 enableEdgeToEdge 函数后,系统状态栏和导航栏会变为透明或半透明,使应用内容可以在其后方显示

MaterialTheme 函数

kotlin 复制代码
@Suppress("DEPRECATION_ERROR")
@Composable
fun MaterialTheme(
    colorScheme: ColorScheme = MaterialTheme.colorScheme,
    shapes: Shapes = MaterialTheme.shapes,
    typography: Typography = MaterialTheme.typography,
    content: @Composable () -> Unit
) {
    val rippleIndication = androidx.compose.material.ripple.rememberRipple()
    val selectionColors = rememberTextSelectionColors(colorScheme)
    CompositionLocalProvider(
        LocalColorScheme provides colorScheme,
        LocalIndication provides rippleIndication,
        androidx.compose.material.ripple.LocalRippleTheme provides MaterialRippleTheme,
        LocalShapes provides shapes,
        LocalTextSelectionColors provides selectionColors,
        LocalTypography provides typography,
    ) {
        ProvideTextStyle(value = typography.bodyLarge, content = content)
    }
}
参数 说明
colorScheme: ColorScheme 主题颜色配置
shapes: Shapes 主题形状配置
typography: Typography 主题排版配置
content: @Composable () -> Unit Compose 函数类型参数,可以把它理解为一块 UI 内容 例如,传入某个页面的 UI 内容,这块 UI 内容就会自动继承当前 MaterialTheme 样式 Unit 表示这个函数没有实际的返回值

remember 函数

1、基本介绍
  • remember 是一个 Composable 函数,用于在 Composable 函数重组时记住一个值
2、演示
  1. 无参数的 remember 函数
kotlin 复制代码
var count by remember { mutableStateOf(0) }

Button(onClick = { count++ }) {
    Text("Count: $count")
}
  1. 带 key 的 remember
kotlin 复制代码
var name by remember { mutableStateOf("tom") }

val content = remember(name) {
    "Hello, $name!"
}

Button(onClick = {
    if (name == "tom") {
        name = "jerry"
    } else if (name == "jerry") {
        name = "tom"
    }
}, content = {
    Text(content)
})
kotlin 复制代码
var name by remember { mutableStateOf("tom") }

var age by remember { mutableStateOf(18) }

val content = remember(name, age) {
    "Hello, $name! You are $age years old."
}

Button(onClick = {
    name = "jerry"
    age = 20
}, content = {
    Text(content)
})
3、数据类型状态
  1. 基础类型状态
kotlin 复制代码
var inputText by remember { mutableStateOf("") }

Column {
    TextField(
        value = inputText,
        onValueChange = { inputText = it },
        label = { Text(text = "请输入内容") }
    )
    Text("你输入的内容:$inputText")
}
kotlin 复制代码
var isChecked by remember { mutableStateOf(false) }

Column {
    Switch(
        checked = isChecked,
        onCheckedChange = { isChecked = it }
    )
    Text("开关状态:${if (isChecked) "开启" else "关闭"}")
}
  1. 自定义数据类状态,支持存储自定义数据类,修改时必须赋值新的对象,否则 Compose 无法感知状态变化,因为对象引用未改变
kotlin 复制代码
data class User(
    val id: Int,
    val name: String,
    val age: Int
)
kotlin 复制代码
var user by remember {
    mutableStateOf(User(id = 1, name = "张三", age = 20))
}

Column {
    Text("用户ID:${user.id}")
    Text("用户姓名:${user.name}")
    Text("用户年龄:${user.age}")

    Button(onClick = {
        user = user.copy(age = user.age + 1)
    }) {
        Text("年龄 + 1")
    }
}
  1. 集合类型状态,支持存储集合类型,修改时必须赋值新的集合对象,否则 Compose 无法感知状态变化,因为对象引用未改变
kotlin 复制代码
var fruitList by remember {
    mutableStateOf(listOf("苹果", "香蕉", "橙子"))
}

Column {
    fruitList.forEach { fruit ->
        Text(text = fruit)
    }

    Button(onClick = {
        fruitList = fruitList + "葡萄"
    }) {
        Text("添加葡萄")
    }
}

补充学习

1、尾随 Lambda
(1)基本介绍
  • 当一个函数的最后一个参数是函数类型时,并使用 Lambda 表达式作为参数传递时,可以将 Lambda 表达式放在括号外
(2)演示
kotlin 复制代码
fun say_hello(name: String, other_operation: () -> Unit): Unit {
    println("你好,$name")
    other_operation()
}

say_hello("张三", {
    println("今天的天气不错")
    println("比较适合去旅游")
})

say_hello("李四") {
    println("今天的天气不错")
    println("比较适合去旅游")
}
复制代码
# 输出结果

你好,张三
今天的天气不错
比较适合去旅游
你好,李四
今天的天气不错
比较适合去旅游
kotlin 复制代码
fun test(operation: () -> Unit): Unit {
    println("测试开始")
    operation()
    println("测试结束")
}

test {
    println("测试操作...")
}
复制代码
# 输出结果

测试开始
测试操作...
测试结束
kotlin 复制代码
fun custom_compute(operation: (Int, Int) -> Int): Unit {
    println("计算开始")
    val result = operation(10, 20)
    println("计算结果:$result")
}

custom_compute { a, b ->
    a + b
}
复制代码
# 输出结果

计算开始
计算结果:30
传递函数类型参数
  1. 使用 Lambda 表达式
kotlin 复制代码
fun custom_compute(operation: (Int, Int) -> Int): Unit {
    println("计算开始")
    val result = operation(10, 20)
    println("计算结果:$result")
}

custom_compute { a, b ->
    a + b
}
复制代码
# 输出结果

计算开始
计算结果:30
  1. 使用函数引用
kotlin 复制代码
fun custom_compute(operation: (Int, Int) -> Int): Unit {
    println("计算开始")
    val result = operation(10, 20)
    println("计算结果:$result")
}

fun custom_compute_operation(a: Int, b: Int): Int {
    return a * b
}

custom_compute(::custom_compute_operation)
复制代码
# 输出结果

计算开始
计算结果:200
  1. 使用匿名函数
kotlin 复制代码
fun custom_compute(operation: (Int, Int) -> Int): Unit {
    println("计算开始")
    val result = operation(10, 20)
    println("计算结果:$result")
}

custom_compute(fun(a: Int, b: Int): Int {
    return a - b
})
复制代码
# 输出结果

计算开始
计算结果:-10
相关推荐
工具罗某人5 小时前
docker快速部署minio
java·nginx·docker
2501_941877136 小时前
大规模系统稳定性建设方法论与工程实践分享
java·开发语言
学习在路上ing6 小时前
ollama部署模型
java·ollama
浩瀚地学6 小时前
【Java】面向对象进阶-接口
java·开发语言·经验分享·笔记·学习
沛沛老爹6 小时前
用 Web 开发思维理解 Agent 的三大支柱——Tools + Memory + LLM
java·人工智能·llm·llama·rag
组合缺一6 小时前
灵动如画 —— 初识 Solon Graph Fluent API 编排
java·solon·graph·flow·langgraph·liquor
强子感冒了6 小时前
Java Map学习笔记:HashMap、LinkedHashMap 与 TreeMap 的核心使用与区别
java·笔记·学习
洛枫偃月6 小时前
SpringBoot3.5.x集成Nacos 2.x全流程详解
java·spring boot·后端
haokan_Jia6 小时前
Java 并发编程-ScheduledFuture
java·前端·python
亓才孓6 小时前
封装类对象的缓存对象
java·jvm·缓存