小编Pit*_*tos的帖子

在 String? 类型的可空接收器上只允许安全 (?.) 或非空断言 (!!.) 调用?

fun checkLengthA(str : String?): Int = if (str.isNullOrBlank()) 0 else str.length
Run Code Online (Sandbox Code Playgroud)

“在 String 类型的可空接收器上只允许安全 (?.) 或非空断言 (!!.) 调用?

所有空对象(或空)都被 isNullOrBlank() 捕获,因此 str.length 中的 str 对象永远不能为空(或空)。这可以通过用显式检查替换扩展函数来实现。

fun checkLengthB(str : String?): Int = if (str == null) 0 else str.length
Run Code Online (Sandbox Code Playgroud)

或不那么冗长的表达:

fun checkLengthC(str : String?): Int = str?.length ?: 0
Run Code Online (Sandbox Code Playgroud)

checkLengthB 和 checkLengthC 都可以正常运行。

让我们从 checkLengthA 中删除可空类型以避免编译错误,如上所示:

fun checkLengthA(str : String): Int = if (str.isNullOrBlank()) 0 else str.length
Run Code Online (Sandbox Code Playgroud)

现在,我们只允许解析使用 String 类型的非空参数,所以如果我们期望一些空类型,那么我们必须把“?” 背部。

看起来编译器不明白在运行 str.length 和扩展函数时,String 类型的 str 永远不会计算为 null,但是如果我们在 if-else 语句中使用 (str …

kotlin

4
推荐指数
1
解决办法
4438
查看次数

如何在 Jetpack Compose 中限制图像平移到框的边缘?

如何在 Compose 中限制图像平移到框的边缘?

我用来pointerInput(Unit) { detectTransformGestures { centroid, pan, zoom, rotation -> }}控制缩放和平移。

当图像最小化为 时,我正在解决这个平移1f问题if (scale.value == 1f) 0f else panOffsetX。我想对放大的图像执行相同的操作(1f <scale <= 3f)

Box(
    modifier = Modifier
        .clip(RectangleShape)
        .fillMaxWidth()
        .background(Color.Gray)
        .pointerInput(Unit) {
            detectTransformGestures { centroid, pan, zoom, rotation ->
                val constraintZoom = when {
                    scale.value > 3f -> 3f
                    scale.value < 1f -> 1f
                    else -> (scale.value * zoom)
                }
                scale.value = constraintZoom
                panOffset += pan
                panOffsetX += pan.x
                panOffsetY += pan.y
                centroidOffset …
Run Code Online (Sandbox Code Playgroud)

android android-jetpack-compose android-jetpack-compose-gesture

2
推荐指数
1
解决办法
575
查看次数

显示两种颜色(圆圈)的 FloatingActionButton

我在布局中添加了一个 FloatingActionButton,其唯一的父级是 CoordinatorLayout,所以我不明白 backgroundTint 颜色来自哪里。

我尝试更改颜色以匹配内圆,但它将整个按钮更改为纯色。

我还应用了不同的样式,但它根本没有改变按钮。我过去解决过这个问题,但我不记得我是怎么做到的。

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_action_add"
        app:layout_anchor="@+id/edit_layout"
        app:layout_anchorGravity="bottom|right|end" />
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

android floating-action-button

0
推荐指数
1
解决办法
955
查看次数