KTX 扩展列表

androidx.activity

依赖项

Groovy

dependencies {
    implementation "androidx.activity:activity-ktx:1.8.2"
}

Kotlin

dependencies {
    implementation("androidx.activity:activity-ktx:1.8.2")
}

扩展函数

对于 OnBackPressedDispatcher
OnBackPressedCallback OnBackPressedDispatcher.addCallback(owner: LifecycleOwner? = null, enabled: Boolean = true, onBackPressed: OnBackPressedCallback.() -> Unit)

创建并添加新的 OnBackPressedCallback,它会调用 OnBackPressedCallback.handleOnBackPressed 中的 onBackPressed

对于 ComponentActivity
Lazy<VM> ComponentActivity.viewModels(noinline factoryProducer: () -> ViewModelProvider.Factory = null)

返回一个延迟委托以访问 ComponentActivity 的 ViewModel。如果已指定 factoryProducer,则它返回的 ViewModelProvider.Factory 将用于首次创建 ViewModel

androidx.benchmark

依赖项

Groovy

dependencies {
    implementation "androidx.benchmark:benchmark-junit4:1.2.1"
}

Kotlin

dependencies {
    implementation("androidx.benchmark:benchmark-junit4:1.2.1")
}

顶级函数

Unit beginTraceSection(sectionName: String)

Unit endTraceSection()

androidx.benchmark.junit4

依赖项

Groovy

dependencies {
    implementation "androidx.benchmark:benchmark-junit4:1.2.1"
}

Kotlin

dependencies {
    implementation("androidx.benchmark:benchmark-junit4:1.2.1")
}

扩展函数

对于 BenchmarkRule
Unit BenchmarkRule.measureRepeated(crossinline block: BenchmarkRule.Scope.() -> Unit)

对代码块进行基准测试。

androidx.collection

依赖项

Groovy

dependencies {
    implementation "androidx.collection:collection-ktx:1.3.0"
}

Kotlin

dependencies {
    implementation("androidx.collection:collection-ktx:1.3.0")
}

扩展函数

对于 LongSparseArray
运算符 Boolean LongSparseArray<T>.contains(key: Long)

如果该集合包含 key,则返回 true。

Unit LongSparseArray<T>.forEach(action: (key: Long, value: T) -> Unit)

针对各个键值对条目执行指定的操作

T LongSparseArray<T>.getOrDefault(key: Long, defaultValue: T)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue

T LongSparseArray<T>.getOrElse(key: Long, defaultValue: () -> T)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue 中的值。

Boolean LongSparseArray<T>.isNotEmpty()

当集合中包含多个元素时,返回 true。

LongIterator LongSparseArray<T>.keyIterator()

针对该集合的键返回迭代器。

运算符 LongSparseArray<T> LongSparseArray<T>.plus(other: LongSparseArray<T>)

通过添加或替换 other 中的条目来创建一个新集合。

Boolean LongSparseArray<T>.remove(key: Long, value: T)

仅当 key 的条目映射到 value 时,才移除该条目。

运算符 Unit LongSparseArray<T>.set(key: Long, value: T)

允许使用索引运算符在集合中存储值。

Iterator<T> LongSparseArray<T>.valueIterator()

针对该集合的值返回迭代器。

对于 SparseArrayCompat
运算符 Boolean SparseArrayCompat<T>.contains(key: Int)

如果该集合包含 key,则返回 true。

Unit SparseArrayCompat<T>.forEach(action: (key: Int, value: T) -> Unit)

针对各个键值对条目执行指定的操作

T SparseArrayCompat<T>.getOrDefault(key: Int, defaultValue: T)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue

T SparseArrayCompat<T>.getOrElse(key: Int, defaultValue: () -> T)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue 中的值。

Boolean SparseArrayCompat<T>.isNotEmpty()

当集合中包含多个元素时,返回 true。

IntIterator SparseArrayCompat<T>.keyIterator()

针对该集合的键返回迭代器。

运算符 SparseArrayCompat<T> SparseArrayCompat<T>.plus(other: SparseArrayCompat<T>)

通过添加或替换 other 中的条目来创建一个新集合。

Boolean SparseArrayCompat<T>.remove(key: Int, value: T)

仅当 key 的条目映射到 value 时,才移除该条目。

运算符 Unit SparseArrayCompat<T>.set(key: Int, value: T)

允许使用索引运算符在集合中存储值。

Iterator<T> SparseArrayCompat<T>.valueIterator()

针对该集合的值返回迭代器。

扩展属性

对于 LongSparseArray
Int LongSparseArray<T>.size()

返回集合中键/值对的数量。

对于 SparseArrayCompat
Int SparseArrayCompat<T>.size()

返回集合中键/值对的数量。

顶级函数

ArrayMap<K, V> arrayMapOf()

返回新的空 ArrayMap

ArrayMap<K, V> arrayMapOf(vararg pairs: Pair<K, V>)

返回以键值对列表形式提供的具有指定内容的新 ArrayMap,键值对的第一个组成部分是键,第二个组成部分是值。

ArraySet<T> arraySetOf()

返回新的空 ArraySet

ArraySet<T> arraySetOf(vararg values: T)

返回具有指定内容的新 ArraySet

LruCache<K, V> lruCache(maxSize: Int, crossinline sizeOf: (key: K, value: V) -> Int = { _, _ -> 1 }, crossinline create: (key: K) -> V? = { null as V? }, crossinline onEntryRemoved: (evicted: Boolean, key: K, oldValue: V, newValue: V?) -> Unit = { _, _, _, _ -> })

创建具有指定参数的 LruCache

androidx.core.animation

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.animation.Animator
AnimatorListener Animator.addListener(crossinline onEnd: (animator: Animator) -> Unit = {}, crossinline onStart: (animator: Animator) -> Unit = {}, crossinline onCancel: (animator: Animator) -> Unit = {}, crossinline onRepeat: (animator: Animator) -> Unit = {})

使用提供的操作向此 Animator 添加监听器。

AnimatorPauseListener Animator.addPauseListener(crossinline onResume: (animator: Animator) -> Unit = {}, crossinline onPause: (animator: Animator) -> Unit = {})

使用提供的操作向此 Animator 添加暂停和继续播放监听器。

AnimatorListener Animator.doOnCancel(crossinline action: (animator: Animator) -> Unit)

添加将在动画取消播放时调用的操作。

AnimatorListener Animator.doOnEnd(crossinline action: (animator: Animator) -> Unit)

添加将在动画结束播放时调用的操作。

AnimatorPauseListener Animator.doOnPause(crossinline action: (animator: Animator) -> Unit)

添加将在动画暂停播放时调用的操作。

AnimatorListener Animator.doOnRepeat(crossinline action: (animator: Animator) -> Unit)

添加将在动画重复播放时调用的操作。

AnimatorPauseListener Animator.doOnResume(crossinline action: (animator: Animator) -> Unit)

添加将在动画暂停后继续播放时调用的操作。

AnimatorListener Animator.doOnStart(crossinline action: (animator: Animator) -> Unit)

添加将在动画开始播放时调用的操作。

androidx.core.content

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.content.Context
T? Context.getSystemService()

将句柄按类别返回到系统级服务。

Unit Context.withStyledAttributes(set: AttributeSet? = null, attrs: IntArray, @AttrRes defStyleAttr: Int = 0, @StyleRes defStyleRes: Int = 0, block: TypedArray.() -> Unit)

TypedArray 接收器上执行 block

Unit Context.withStyledAttributes(@StyleRes resourceId: Int, attrs: IntArray, block: TypedArray.() -> Unit)

TypedArray 接收器上执行 block

对于 android.content.SharedPreferences
Unit SharedPreferences.edit(commit: Boolean = false, action: Editor.() -> Unit)

允许通过调用应用提交保留更改,从而修改此偏好设置实例。

顶级函数

ContentValues contentValuesOf(vararg pairs: Pair<String, Any?>)

返回以指定键值对作为元素的新 ContentValues

androidx.core.content.res

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.content.res.TypedArray
Boolean TypedArray.getBooleanOrThrow(@StyleableRes index: Int)

检索索引中属性的布尔值,或者如果未定义该值,则抛出 IllegalArgumentException

Int TypedArray.getColorOrThrow(@StyleableRes index: Int)

检索索引中属性的颜色值,或者如果未定义该值,则抛出 IllegalArgumentException

ColorStateList TypedArray.getColorStateListOrThrow(@StyleableRes index: Int)

检索索引中属性的颜色状态列表值,或者如果未定义该值,则抛出 IllegalArgumentException

Float TypedArray.getDimensionOrThrow(@StyleableRes index: Int)

检索索引中属性的尺寸值,或者如果未定义该值,则抛出 IllegalArgumentException

Int TypedArray.getDimensionPixelOffsetOrThrow(@StyleableRes index: Int)

检索索引中属性的尺寸像素偏移值,或者如果未定义该值,则抛出 IllegalArgumentException

Int TypedArray.getDimensionPixelSizeOrThrow(@StyleableRes index: Int)

检索索引中属性的尺寸像素大小值,或者如果未定义该值,则抛出 IllegalArgumentException

Drawable TypedArray.getDrawableOrThrow(@StyleableRes index: Int)

检索索引中属性的可绘制值,或者如果未定义该值,则抛出 IllegalArgumentException

Float TypedArray.getFloatOrThrow(@StyleableRes index: Int)

检索索引中属性的浮点值,或者如果未定义该值,则抛出 IllegalArgumentException

Typeface TypedArray.getFontOrThrow(@StyleableRes index: Int)

检索索引中属性的字体值,或者如果未定义该值,则抛出 IllegalArgumentException

Int TypedArray.getIntOrThrow(@StyleableRes index: Int)

检索索引中属性的整数值,或者如果未定义该值,则抛出 IllegalArgumentException

Int TypedArray.getIntegerOrThrow(@StyleableRes index: Int)

检索索引中属性的整数值,或者如果未定义该值,则抛出 IllegalArgumentException

Int TypedArray.getResourceIdOrThrow(@StyleableRes index: Int)

检索索引中属性的资源标识符,或者如果未定义该值,则抛出 IllegalArgumentException

String TypedArray.getStringOrThrow(@StyleableRes index: Int)

检索索引中属性的字符串值,或者如果未定义该值,则抛出 IllegalArgumentException

Array<CharSequence> TypedArray.getTextArrayOrThrow(@StyleableRes index: Int)

检索索引中属性的文本数组值,或者如果未定义该值,则抛出 IllegalArgumentException

CharSequence TypedArray.getTextOrThrow(@StyleableRes index: Int)

检索索引中属性的文本值,或者如果未定义该值,则抛出 IllegalArgumentException

R TypedArray.use(block: (TypedArray) -> R)

针对此 TypedArray 执行指定的 block 函数,然后对其进行回收。

androidx.core.database

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.database.Cursor
ByteArray? Cursor.getBlobOrNull(index: Int)

返回所请求列的值作为可为 Null 的字节数组。

Double? Cursor.getDoubleOrNull(index: Int)

返回所请求列的值作为可为 Null 的 double 值。

Float? Cursor.getFloatOrNull(index: Int)

返回所请求列的值作为可为 Null 的浮点值。

Int? Cursor.getIntOrNull(index: Int)

返回所请求列的值作为可为 Null 的整数值。

Long? Cursor.getLongOrNull(index: Int)

返回所请求列的值作为可为 Null 的 long 值。

Short? Cursor.getShortOrNull(index: Int)

返回所请求列的值作为可为 Null 的 short 值。

String? Cursor.getStringOrNull(index: Int)

返回所请求列的值作为可为 Null 的字符串。

androidx.core.database.sqlite

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.database.sqlite.SQLiteDatabase
T SQLiteDatabase.transaction(exclusive: Boolean = true, body: SQLiteDatabase.() -> T)

在事务中运行 body,如果顺利完成,则将其标记为“成功”。

androidx.core.graphics

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.graphics.Bitmap
Bitmap Bitmap.applyCanvas(block: Canvas.() -> Unit)

创建新的画布以在该位图上进行绘制,并在新创建的画布上执行指定的 block

运算符 Boolean Bitmap.contains(p: Point)

如果指定的点位于该位图内部,则返回 true。

运算符 Boolean Bitmap.contains(p: PointF)

如果指定的点位于该位图内部,则返回 true。

运算符 Int Bitmap.get(x: Int, y: Int)

返回指定位置的像素值。

Bitmap Bitmap.scale(width: Int, height: Int, filter: Boolean = true)

创建一个新的位图,并尽可能通过缩放此位图来创建。

运算符 Unit Bitmap.set(x: Int, y: Int, color: Int)

将指定的 color int 写入位于指定的 (x, y) 坐标处的位图(假设它是可变的)。

对于 android.graphics.Canvas
Unit Canvas.withClip(clipRect: Rect, block: Canvas.() -> Unit)

将指定的 block 封装在对 Canvas.save/Canvas.clipRectCanvas.restoreToCount 的调用中。

Unit Canvas.withClip(clipRect: RectF, block: Canvas.() -> Unit)

将指定的 block 封装在对 Canvas.save/Canvas.clipRectCanvas.restoreToCount 的调用中。

Unit Canvas.withClip(left: Int, top: Int, right: Int, bottom: Int, block: Canvas.() -> Unit)

将指定的 block 封装在对 Canvas.save/Canvas.clipRectCanvas.restoreToCount 的调用中。

Unit Canvas.withClip(left: Float, top: Float, right: Float, bottom: Float, block: Canvas.() -> Unit)

将指定的 block 封装在对 Canvas.save/Canvas.clipRectCanvas.restoreToCount 的调用中。

Unit Canvas.withClip(clipPath: Path, block: Canvas.() -> Unit)

将指定的 block 封装在对 Canvas.save/Canvas.clipPathCanvas.restoreToCount 的调用中。

Unit Canvas.withMatrix(matrix: Matrix = Matrix(), block: Canvas.() -> Unit)

将指定的 block 封装在对 Canvas.save/Canvas.concatCanvas.restoreToCount 的调用中。

Unit Canvas.withRotation(degrees: Float = 0.0f, pivotX: Float = 0.0f, pivotY: Float = 0.0f, block: Canvas.() -> Unit)

将指定的 block 封装在对 Canvas.save/Canvas.rotateCanvas.restoreToCount 的调用中。

Unit Canvas.withSave(block: Canvas.() -> Unit)

将指定的 block 封装在对 Canvas.saveCanvas.restoreToCount 的调用中。

Unit Canvas.withScale(x: Float = 1.0f, y: Float = 1.0f, pivotX: Float = 0.0f, pivotY: Float = 0.0f, block: Canvas.() -> Unit)

将指定的 block 封装在对 Canvas.save/Canvas.scaleCanvas.restoreToCount 的调用中。

Unit Canvas.withSkew(x: Float = 0.0f, y: Float = 0.0f, block: Canvas.() -> Unit)

将指定的 block 封装在对 Canvas.save/Canvas.skewCanvas.restoreToCount 的调用中。

Unit Canvas.withTranslation(x: Float = 0.0f, y: Float = 0.0f, block: Canvas.() -> Unit)

将指定的 block 封装在对 Canvas.save/Canvas.translateCanvas.restoreToCount 的调用中。

对于 android.graphics.Color
运算符 Float Color.component1()

返回颜色的第一个分量。

运算符 Float Color.component2()

返回颜色的第二个分量。

运算符 Float Color.component3()

返回颜色的第三个分量。

运算符 Float Color.component4()

返回颜色的第四个分量。

infix Color! Color.convertTo(colorSpace: Named)

将颜色接收器转换为指定颜色空间中的颜色。

infix Color! Color.convertTo(colorSpace: ColorSpace)

将颜色接收器转换为指定颜色空间中的颜色。

运算符 Color Color.plus(c: Color)

将两种半透明颜色混合在一起。

对于 android.graphics.ImageDecoder.Source
Bitmap Source.decodeBitmap(crossinline action: ImageDecoder.(info: ImageInfo, source: Source) -> Unit)

从来源创建位图

Drawable Source.decodeDrawable(crossinline action: ImageDecoder.(info: ImageInfo, source: Source) -> Unit)

从来源创建可绘制对象

对于 android.graphics.Matrix
运算符 Matrix Matrix.times(m: Matrix)

将此矩阵与另一个矩阵相乘,并返回结果作为一个新矩阵。

FloatArray Matrix.values()

返回此矩阵的 9 个值作为新的浮点数组。

对于 android.graphics.Paint
Boolean Paint.setBlendMode(blendModeCompat: BlendModeCompat?)

以向后兼容的方式配置 Paint 的 BlendMode 的简便方法。

对于 android.graphics.Path
infix Path Path.and(p: Path)

返回两条路径的交集作为新的路径

Iterable<PathSegment> Path.flatten(error: Float = 0.5f)

通过一系列线段对路径进行扁平化(或近似)处理。

运算符 Path Path.minus(p: Path)

返回两条路径的差异部分作为新的路径

infix Path Path.or(p: Path)

返回两条路径的并集作为新的路径

运算符 Path Path.plus(p: Path)

返回两条路径的并集作为新的路径

infix Path Path.xor(p: Path)

返回两条路径的并集减去交集的部分作为新的路径

对于 android.graphics.Picture
Picture Picture.record(width: Int, height: Int, block: Canvas.() -> Unit)

创建一个新的画布以记录此图片中的命令,然后在新创建的画布上执行指定的 block,并返回该图片

对于 android.graphics.Point
运算符 Int Point.component1()

返回此点的 X 坐标。

运算符 Int Point.component2()

返回此点的 Y 坐标。

运算符 Point Point.minus(p: Point)

通过反向移动指定的点对此点进行偏移,并返回结果作为新的点。

运算符 Point Point.minus(xy: Int)

通过沿 X 轴和 Y 轴方向反向移动指定距离对该点进行偏移,并返回结果作为新的点。

运算符 Point Point.plus(p: Point)

根据指定点对该点进行偏移,并返回结果作为新的点。

运算符 Point Point.plus(xy: Int)

通过沿 X 轴和 Y 轴方向移动指定的距离对此点进行偏移,并返回结果作为新的点。

PointF Point.toPointF()

返回此点的 PointF 表示法。

运算符 Point Point.unaryMinus()

返回表示该点的对立点的新点。

对于 android.graphics.PointF
运算符 Float PointF.component1()

返回此点的 X 坐标。

运算符 Float PointF.component2()

返回此点的 Y 坐标。

运算符 PointF PointF.minus(p: PointF)

通过反向移动指定的点对此点进行偏移,并返回结果作为新的点。

运算符 PointF PointF.minus(xy: Float)

通过沿 X 轴和 Y 轴方向反向移动指定距离对该点进行偏移,并返回结果作为新的点。

运算符 PointF PointF.plus(p: PointF)

根据指定点对该点进行偏移,并返回结果作为新的点。

运算符 PointF PointF.plus(xy: Float)

通过沿 X 轴和 Y 轴方向移动指定的距离对此点进行偏移,并返回结果作为新的点。

Point PointF.toPoint()

返回此点的 Point 表示法。

运算符 PointF PointF.unaryMinus()

返回表示该点的对立点的新点。

对于 android.graphics.PorterDuff.Mode
PorterDuffColorFilter Mode.toColorFilter(color: Int)

创建一个新的 PorterDuffColorFilter,它使用此 PorterDuff.Mode 作为 Alpha 合成或混合模式,并使用指定的颜色

PorterDuffXfermode Mode.toXfermode()

创建一个新的 PorterDuffXfermode,它使用此 PorterDuff.Mode 作为 Alpha 合成或混合模式。

对于 android.graphics.Rect
infix Rect Rect.and(r: Rect)

返回两个矩形的交集作为新的矩形。

运算符 Int Rect.component1()

返回矩形的第一个组成部分“left”。

运算符 Int Rect.component2()

返回矩形的第二个组成部分“top”。

运算符 Int Rect.component3()

返回矩形的第三个组成部分“right”。

运算符 Int Rect.component4()

返回矩形的第四个组成部分“bottom”。

运算符 Boolean Rect.contains(p: Point)

如果指定的点位于该矩形内部,则返回 true。

运算符 Region Rect.minus(r: Rect)

返回此矩形与指定矩形的差异部分作为新的区域。

运算符 Rect Rect.minus(xy: Int)

返回一个新的矩形,它表示该矩形通过沿 X 轴和 Y 轴方向反向移动指定距离进行偏移。

运算符 Rect Rect.minus(xy: Point)

返回一个新的矩形,它表示该矩形通过反向移动指定的点进行偏移。

infix Rect Rect.or(r: Rect)

返回两个矩形的并集作为新的矩形。

运算符 Rect Rect.plus(r: Rect)

执行此矩形与指定矩形的并集,并返回结果作为新的矩形。

运算符 Rect Rect.plus(xy: Int)

返回一个新的矩形,它表示该矩形在 X 轴和 Y 轴上按照指定的量进行偏移。

运算符 Rect Rect.plus(xy: Point)

返回一个新的矩形,它表示该矩形按照指定的点进行偏移。

运算符 Rect Rect.times(factor: Int)

返回一个新的矩形,它表示该矩形的各个组成部分按比例进行了缩放。

RectF Rect.toRectF()

返回该矩形的 RectF 表示法。

Region Rect.toRegion()

返回该矩形的 Region 表示法。

infix Region Rect.xor(r: Rect)

返回两个矩形的并集减去交集的部分作为新的区域。

对于 android.graphics.RectF
infix RectF RectF.and(r: RectF)

返回两个矩形的交集作为新的矩形。

运算符 Float RectF.component1()

返回矩形的第一个组成部分“left”。

运算符 Float RectF.component2()

返回矩形的第二个组成部分“top”。

运算符 Float RectF.component3()

返回矩形的第三个组成部分“right”。

运算符 Float RectF.component4()

返回矩形的第四个组成部分“bottom”。

运算符 Boolean RectF.contains(p: PointF)

如果指定的点位于该矩形内部,则返回 true。

运算符 Region RectF.minus(r: RectF)

返回此矩形与指定矩形的差异部分作为新的区域。

运算符 RectF RectF.minus(xy: Float)

返回一个新的矩形,它表示该矩形通过沿 X 轴和 Y 轴方向反向移动指定距离进行偏移。

运算符 RectF RectF.minus(xy: PointF)

返回一个新的矩形,它表示该矩形通过反向移动指定的点进行偏移。

infix RectF RectF.or(r: RectF)

返回两个矩形的并集作为新的矩形。

运算符 RectF RectF.plus(r: RectF)

执行此矩形与指定矩形的并集,并返回结果作为新的矩形。

运算符 RectF RectF.plus(xy: Float)

返回一个新的矩形,它表示该矩形在 X 轴和 Y 轴上按照指定的量进行偏移。

运算符 RectF RectF.plus(xy: PointF)

返回一个新的矩形,它表示该矩形按照指定的点进行偏移。

运算符 RectF RectF.times(factor: Int)

返回一个新的矩形,它表示该矩形的各个组成部分按比例进行了缩放。

运算符 RectF RectF.times(factor: Float)

返回一个新的矩形,它表示该矩形的各个组成部分按比例进行了缩放。

Rect RectF.toRect()

返回该矩形的 Rect 表示法。

Region RectF.toRegion()

返回该矩形的 Region 表示法。

RectF RectF.transform(m: Matrix)

使用提供的矩阵对此矩形进行适当转换,并返回该矩形。

infix Region RectF.xor(r: RectF)

返回两个矩形的并集减去交集的部分作为新的区域。

对于 android.graphics.Region
infix Region Region.and(r: Rect)

返回该区域与指定矩形的交集作为新的区域。

infix Region Region.and(r: Region)

返回该区域与指定区域的交集作为新的区域。

运算符 Boolean Region.contains(p: Point)

如果该区域包含指定的,则返回 true。

Unit Region.forEach(action: (rect: Rect) -> Unit)

对该区域中的各个矩形执行指定的操作。

运算符 Iterator<Rect> Region.iterator()

针对该区域内的矩形返回迭代器

运算符 Region Region.minus(r: Rect)

返回该区域与指定矩形之间的差异部分作为新的区域。

运算符 Region Region.minus(r: Region)

返回该区域与指定区域的差异部分作为新的区域。

运算符 Region Region.not()

返回该区域的对立区域作为新的区域。

infix Region Region.or(r: Rect)

返回该区域与指定矩形的并集作为新的区域。

infix Region Region.or(r: Region)

返回该区域与指定区域的并集作为新的区域。

运算符 Region Region.plus(r: Rect)

返回该区域与指定矩形的并集作为新的区域。

运算符 Region Region.plus(r: Region)

返回该区域与指定区域的并集作为新的区域。

运算符 Region Region.unaryMinus()

返回该区域的对立区域作为新的区域。

infix Region Region.xor(r: Rect)

返回该区域与指定矩形的并集减去交集的部分作为新的区域。

infix Region Region.xor(r: Region)

返回该区域与指定区域的并集减去交集的部分作为新的区域。

对于 android.graphics.Shader
Unit Shader.transform(block: Matrix.() -> Unit)

将指定的 block 封装在对 Shader.getLocalMatrixShader.setLocalMatrix 的调用中。

对于 kotlin.Int
运算符 Int Int.component1()

返回 color int 的 Alpha 分量。

运算符 Int Int.component2()

返回 color int 的红色分量。

运算符 Int Int.component3()

返回 color int 的绿色分量。

运算符 Int Int.component4()

返回 color int 的蓝色分量。

infix Long Int.convertTo(colorSpace: Named)

将 color int 接收器转换为指定颜色空间中的 color long。

infix Long Int.convertTo(colorSpace: ColorSpace)

将 color int 接收器转换为指定颜色空间中的 color long。

Color Int.toColor()

根据 color int 创建一个新的颜色实例。

Long Int.toColorLong()

将指定的 ARGB color int 转换为 sRGB 颜色空间中的 RGBA color long

对于 kotlin.Long
运算符 Float Long.component1()

返回颜色的第一个分量。

运算符 Float Long.component2()

返回颜色的第二个分量。

运算符 Float Long.component3()

返回颜色的第三个分量。

运算符 Float Long.component4()

返回颜色的第四个分量。

infix Long Long.convertTo(colorSpace: Named)

将 color long 接收器转换为指定颜色空间中的 color long。

infix Long Long.convertTo(colorSpace: ColorSpace)

将 color long 接收器转换为指定颜色空间中的 color long。

Color Long.toColor()

color long 中创建一个新的颜色实例。

Int Long.toColorInt()

将指定的 color long 转换为 ARGB color int

对于 kotlin.String
Int String.toColorInt()

返回此字符串对应的 Int 颜色。

扩展属性

对于 kotlin.Int
Int Int.alpha()

返回 color int 的 Alpha 分量。

Int Int.blue()

返回 color int 的蓝色分量。

Int Int.green()

返回 color int 的绿色分量。

Float Int.luminance()

返回 color int 的相对亮度,假设采用 sRGB 编码。

Int Int.red()

返回 color int 的红色分量。

对于 kotlin.Long
Float Long.alpha()

返回 color long 的 Alpha 分量。

Float Long.blue()

返回 color long 的蓝色分量。

ColorSpace Long.colorSpace()

返回采用指定的 color long 进行编码的颜色空间。

Float Long.green()

返回 color long 的绿色分量。

Boolean Long.isSrgb()

表明该颜色是否在 sRGB 颜色空间内。

Boolean Long.isWideGamut()

表明该颜色是否在广色域颜色空间内。

Float Long.luminance()

返回颜色的相对亮度。

Float Long.red()

返回 color long 的红色分量。

顶级函数

Bitmap createBitmap(width: Int, height: Int, config: Config = Bitmap.Config.ARGB_8888)

返回具有指定宽度高度的可变位图。

Bitmap createBitmap(width: Int, height: Int, config: Config = Bitmap.Config.ARGB_8888, hasAlpha: Boolean = true, colorSpace: ColorSpace = ColorSpace.get(ColorSpace.Named.SRGB))

返回具有指定宽度高度的可变位图。

Matrix rotationMatrix(degrees: Float, px: Float = 0.0f, py: Float = 0.0f)

创建一个旋转矩阵,该矩阵由围绕位于坐标(pxpy)处的轴心点的旋转角度(以度为单位)定义。

Matrix scaleMatrix(sx: Float = 1.0f, sy: Float = 1.0f)

创建一个缩放矩阵,该矩阵在 x 轴和 y 轴上的缩放比例分别为 sxsy

Matrix translationMatrix(tx: Float = 0.0f, ty: Float = 0.0f)

创建一个平移矩阵,该矩阵在 x 轴和 y 轴上的平移量分别为 txty

androidx.core.graphics.drawable

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.graphics.Bitmap
Icon Bitmap.toAdaptiveIcon()

根据该自适应位图创建一个图标

BitmapDrawable Bitmap.toDrawable(resources: Resources)

根据该位图创建一个 BitmapDrawable

Icon Bitmap.toIcon()

根据该位图创建一个图标

对于 android.graphics.Color
ColorDrawable Color.toDrawable()

根据该颜色创建一个 ColorDrawable(通过 Color.toArgb)。

对于 android.graphics.drawable.Drawable
Bitmap Drawable.toBitmap(@Px width: Int = intrinsicWidth, @Px height: Int = intrinsicHeight, config: Config? = null)

返回该可绘制对象位图表示法。

Unit Drawable.updateBounds(@Px left: Int = bounds.left, @Px top: Int = bounds.top, @Px right: Int = bounds.right, @Px bottom: Int = bounds.bottom)

更新该可绘制对象的边界。

对于 android.net.Uri
Icon Uri.toIcon()

根据该 URI 创建一个图标

对于 kotlin.ByteArray
Icon ByteArray.toIcon()

根据该 ByteArray 创建一个图标

对于 kotlin.Int
ColorDrawable Int.toDrawable()

根据该颜色值创建一个 ColorDrawable

androidx.core.location

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.location.Location
运算符 Double Location.component1()

返回此位置的纬度。

运算符 Double Location.component2()

返回此位置的经度。

androidx.core.net

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.net.Uri
File Uri.toFile()

根据指定的 URI 创建一个文件

对于 java.io.File
Uri File.toUri()

根据指定的文件创建一个 URI。

对于 kotlin.String
Uri String.toUri()

根据指定的编码 URI 字符串创建一个 URI。

androidx.core.os

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.os.Handler
Runnable Handler.postAtTime(uptimeMillis: Long, token: Any? = null, crossinline action: () -> Unit)

Handler.postAtTime 的一个版本,用于对参数重新排序,允许将操作置于括号之外。

Runnable Handler.postDelayed(delayInMillis: Long, token: Any? = null, crossinline action: () -> Unit)

Handler.postDelayed 的一个版本,用于对参数重新排序,允许将操作置于括号之外。

顶级函数

Bundle bundleOf(vararg pairs: Pair<String, Any?>)

返回以指定键值对作为元素的新 Bundle

PersistableBundle persistableBundleOf(vararg pairs: Pair<String, Any?>)

返回以指定键值对作为元素的新 PersistableBundle

T trace(sectionName: String, block: () -> T)

将指定的 block 封装在对 Trace.beginSection(具有提供的 sectionName)和 Trace.endSection 的调用中。

androidx.core.text

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.text.Spannable
Unit Spannable.clearSpans()

清除此文本中的所有 span。

运算符 Unit Spannable.set(start: Int, end: Int, span: Any)

span 添加到文本的开始&hellip;结束范围内。

运算符 Unit Spannable.set(range: IntRange, span: Any)

span 添加到文本的 range 内。

对于 android.text.SpannableStringBuilder
SpannableStringBuilder SpannableStringBuilder.backgroundColor(color: Int, builderAction: SpannableStringBuilder.() -> Unit)

builderAction 中的附加文本封装到 BackgroundColorSpan 中。

SpannableStringBuilder SpannableStringBuilder.bold(builderAction: SpannableStringBuilder.() -> Unit)

builderAction 中的附加文本封装到粗体 StyleSpan 中。

SpannableStringBuilder SpannableStringBuilder.color(color: Int, builderAction: SpannableStringBuilder.() -> Unit)

builderAction 中的附加文字封装到 ForegroundColorSpan 中。

SpannableStringBuilder SpannableStringBuilder.inSpans(vararg spans: Any, builderAction: SpannableStringBuilder.() -> Unit)

builderAction 中的附加文本封装到 span 中。

SpannableStringBuilder SpannableStringBuilder.inSpans(span: Any, builderAction: SpannableStringBuilder.() -> Unit)

builderAction 中的附加文本封装到 span 中。

SpannableStringBuilder SpannableStringBuilder.italic(builderAction: SpannableStringBuilder.() -> Unit)

builderAction 中的附加文本封装到斜体 StyleSpan 中。

SpannableStringBuilder SpannableStringBuilder.scale(proportion: Float, builderAction: SpannableStringBuilder.() -> Unit)

builderAction 中的附加文本封装到 RelativeSizeSpan 中。

SpannableStringBuilder SpannableStringBuilder.strikeThrough(builderAction: SpannableStringBuilder.() -> Unit)

builderAction 中的附加文本封装到 StrikethroughSpan 中。

SpannableStringBuilder SpannableStringBuilder.subscript(builderAction: SpannableStringBuilder.() -> Unit)

builderAction 中的附加文字封装到 SubscriptSpan 中。

SpannableStringBuilder SpannableStringBuilder.superscript(builderAction: SpannableStringBuilder.() -> Unit)

builderAction 中的附加文本封装到 SuperscriptSpan 中。

SpannableStringBuilder SpannableStringBuilder.underline(builderAction: SpannableStringBuilder.() -> Unit)

builderAction 中的附加文本封装到 UnderlineSpan 中。

对于 android.text.Spanned
Array<out T> Spanned.getSpans(start: Int = 0, end: Int = length)

获取为 T 的实例的所有 span。

String Spanned.toHtml(option: Int = TO_HTML_PARAGRAPH_LINES_CONSECUTIVE)

返回此 Spanned 中 span 的 HTML 字符串。

对于 kotlin.CharSequence
Boolean CharSequence.isDigitsOnly()

返回指定的 CharSequence 是否仅包含数字。

Spannable CharSequence.toSpannable()

CharSequence 中返回一个新的 Spannable,或者如果来源已是 SpannableString 的实例,则返回来源本身。

Spanned CharSequence.toSpanned()

CharSequence 中返回一个新的 Spanned,或者如果来源已是 SpannedString 的实例,则返回来源本身。

Int CharSequence.trimmedLength()

如果通过 String.trim 从开始和结束位置对空格和 ASCII 控件字符进行修剪,则返回指定的 CharSequence 的长度。

对于 kotlin.String
String String.htmlEncode()

对字符串进行 HTML 编码。

Spanned String.parseAsHtml(flags: Int = FROM_HTML_MODE_LEGACY, imageGetter: ImageGetter? = null, tagHandler: TagHandler? = null)

通过将此字符串解析为 HTML,返回 Spanned

扩展属性

对于 java.util.Locale
Int Locale.layoutDirection()

返回指定语言区域的布局方向。

顶级函数

SpannedString buildSpannedString(builderAction: SpannableStringBuilder.() -> Unit)

使用提供的 builderAction 填充新创建的 SpannableStringBuilder,并将其转换为 SpannedString,从而构建新的字符串。

androidx.core.transition

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.transition.Transition
TransitionListener Transition.addListener(crossinline onEnd: (transition: Transition) -> Unit = {}, crossinline onStart: (transition: Transition) -> Unit = {}, crossinline onCancel: (transition: Transition) -> Unit = {}, crossinline onResume: (transition: Transition) -> Unit = {}, crossinline onPause: (transition: Transition) -> Unit = {})

使用提供的操作为此转换添加监听器。

TransitionListener Transition.doOnCancel(crossinline action: (transition: Transition) -> Unit)

添加将在此转换被取消时调用的操作。

TransitionListener Transition.doOnEnd(crossinline action: (transition: Transition) -> Unit)

添加将在此转换结束时调用的操作。

TransitionListener Transition.doOnPause(crossinline action: (transition: Transition) -> Unit)

添加将在此转换暂停时调用的操作。

TransitionListener Transition.doOnResume(crossinline action: (transition: Transition) -> Unit)

添加将在此转换暂停后再次继续进行时调用的操作。

TransitionListener Transition.doOnStart(crossinline action: (transition: Transition) -> Unit)

添加将在此转换开始时调用的操作。

androidx.core.util

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.util.AtomicFile
ByteArray AtomicFile.readBytes()

以字节数组的形式获取此文件的全部内容。

String AtomicFile.readText(charset: Charset = Charsets.UTF_8)

使用 UTF-8 或指定的字符集以字符串形式获取此文件的全部内容。

Unit AtomicFile.tryWrite(block: (out: FileOutputStream) -> Unit)

在此文件的 block 内执行写入操作。

Unit AtomicFile.writeBytes(array: ByteArray)

将此文件的内容设置为字节数组

Unit AtomicFile.writeText(text: String, charset: Charset = Charsets.UTF_8)

将此文件的内容设置为使用 UTF-8 或指定的字符集编码的文本

对于 android.util.LongSparseArray
运算符 Boolean LongSparseArray<T>.contains(key: Long)

如果该集合包含 key,则返回 true。

Boolean LongSparseArray<T>.containsKey(key: Long)

如果该集合包含 key,则返回 true。

Boolean LongSparseArray<T>.containsValue(value: T)

如果该集合包含 value,则返回 true。

Unit LongSparseArray<T>.forEach(action: (key: Long, value: T) -> Unit)

针对各个键值对条目执行指定的操作

T LongSparseArray<T>.getOrDefault(key: Long, defaultValue: T)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue

T LongSparseArray<T>.getOrElse(key: Long, defaultValue: () -> T)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue 中的值。

Boolean LongSparseArray<T>.isEmpty()

当集合中未包含任何元素时,返回 true。

Boolean LongSparseArray<T>.isNotEmpty()

当集合中包含多个元素时,返回 true。

LongIterator LongSparseArray<T>.keyIterator()

针对该集合的键返回迭代器。

运算符 LongSparseArray<T> LongSparseArray<T>.plus(other: LongSparseArray<T>)

通过添加或替换 other 中的条目来创建一个新集合。

Unit LongSparseArray<T>.putAll(other: LongSparseArray<T>)

通过添加或替换 other 中的条目来更新此集合。

Boolean LongSparseArray<T>.remove(key: Long, value: T)

仅当 key 的条目映射到 value 时,才移除该条目。

运算符 Unit LongSparseArray<T>.set(key: Long, value: T)

允许使用索引运算符在集合中存储值。

Iterator<T> LongSparseArray<T>.valueIterator()

针对该集合的值返回迭代器。

对于 android.util.Pair
运算符 F Pair<F, S>.component1()

返回键值对的第一个组成部分。

运算符 S Pair<F, S>.component2()

返回键值对的第二个组成部分。

Pair<F, S> Pair<F, S>.toKotlinPair()

将此键值对作为 kotlin.Pair 返回。

对于 android.util.Range
infix Range<T> Range<T>.and(other: Range<T>)

返回此范围与 other 的交集。

运算符 Range<T> Range<T>.plus(value: T)

返回包含该范围和 value 的最小范围。

运算符 Range<T> Range<T>.plus(other: Range<T>)

返回包含该范围和 other 的最小范围。

ClosedRange<T> Range<T>.toClosedRange()

将此 Rang 作为 ClosedRange 返回。

对于 android.util.Size
运算符 Int Size.component1()

返回该尺寸的第一个组成部分“width”。

运算符 Int Size.component2()

返回该尺寸的第二个组成部分“height”。

对于 android.util.SizeF
运算符 Float SizeF.component1()

返回该 SizeF 的第一个组成部分“width”。

运算符 Float SizeF.component2()

返回该 SizeF 的第二个组成部分“height”。

对于 android.util.SparseArray
运算符 Boolean SparseArray<T>.contains(key: Int)

如果该集合包含 key,则返回 true。

Boolean SparseArray<T>.containsKey(key: Int)

如果该集合包含 key,则返回 true。

Boolean SparseArray<T>.containsValue(value: T)

如果该集合包含 value,则返回 true。

Unit SparseArray<T>.forEach(action: (key: Int, value: T) -> Unit)

针对各个键值对条目执行指定的操作

T SparseArray<T>.getOrDefault(key: Int, defaultValue: T)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue

T SparseArray<T>.getOrElse(key: Int, defaultValue: () -> T)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue 中的值。

Boolean SparseArray<T>.isEmpty()

当集合中未包含任何元素时,返回 true。

Boolean SparseArray<T>.isNotEmpty()

当集合中包含多个元素时,返回 true。

IntIterator SparseArray<T>.keyIterator()

针对该集合的键返回迭代器。

运算符 SparseArray<T> SparseArray<T>.plus(other: SparseArray<T>)

通过添加或替换 other 中的条目来创建一个新集合。

Unit SparseArray<T>.putAll(other: SparseArray<T>)

通过添加或替换 other 中的条目来更新此集合。

Boolean SparseArray<T>.remove(key: Int, value: T)

仅当 key 的条目映射到 value 时,才移除该条目。

运算符 Unit SparseArray<T>.set(key: Int, value: T)

允许使用索引运算符在集合中存储值。

Iterator<T> SparseArray<T>.valueIterator()

针对该集合的值返回迭代器。

对于 android.util.SparseBooleanArray
运算符 Boolean SparseBooleanArray.contains(key: Int)

如果该集合包含 key,则返回 true。

Boolean SparseBooleanArray.containsKey(key: Int)

如果该集合包含 key,则返回 true。

Boolean SparseBooleanArray.containsValue(value: Boolean)

如果该集合包含 value,则返回 true。

Unit SparseBooleanArray.forEach(action: (key: Int, value: Boolean) -> Unit)

针对各个键值对条目执行指定的操作

Boolean SparseBooleanArray.getOrDefault(key: Int, defaultValue: Boolean)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue

Boolean SparseBooleanArray.getOrElse(key: Int, defaultValue: () -> Boolean)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue 中的值。

Boolean SparseBooleanArray.isEmpty()

当集合中未包含任何元素时,返回 true。

Boolean SparseBooleanArray.isNotEmpty()

当集合中包含多个元素时,返回 true。

IntIterator SparseBooleanArray.keyIterator()

针对该集合的键返回迭代器。

运算符 SparseBooleanArray SparseBooleanArray.plus(other: SparseBooleanArray)

通过添加或替换 other 中的条目来创建一个新集合。

Unit SparseBooleanArray.putAll(other: SparseBooleanArray)

通过添加或替换 other 中的条目来更新此集合。

Boolean SparseBooleanArray.remove(key: Int, value: Boolean)

仅当 key 的条目映射到 value 时,才移除该条目。

运算符 Unit SparseBooleanArray.set(key: Int, value: Boolean)

允许使用索引运算符在集合中存储值。

BooleanIterator SparseBooleanArray.valueIterator()

针对该集合的值返回迭代器。

对于 android.util.SparseIntArray
运算符 Boolean SparseIntArray.contains(key: Int)

如果该集合包含 key,则返回 true。

Boolean SparseIntArray.containsKey(key: Int)

如果该集合包含 key,则返回 true。

Boolean SparseIntArray.containsValue(value: Int)

如果该集合包含 value,则返回 true。

Unit SparseIntArray.forEach(action: (key: Int, value: Int) -> Unit)

针对各个键值对条目执行指定的操作

Int SparseIntArray.getOrDefault(key: Int, defaultValue: Int)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue

Int SparseIntArray.getOrElse(key: Int, defaultValue: () -> Int)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue 中的值。

Boolean SparseIntArray.isEmpty()

当集合中未包含任何元素时,返回 true。

Boolean SparseIntArray.isNotEmpty()

当集合中包含多个元素时,返回 true。

IntIterator SparseIntArray.keyIterator()

针对该集合的键返回迭代器。

运算符 SparseIntArray SparseIntArray.plus(other: SparseIntArray)

通过添加或替换 other 中的条目来创建一个新集合。

Unit SparseIntArray.putAll(other: SparseIntArray)

通过添加或替换 other 中的条目来更新此集合。

Boolean SparseIntArray.remove(key: Int, value: Int)

仅当 key 的条目映射到 value 时,才移除该条目。

运算符 Unit SparseIntArray.set(key: Int, value: Int)

允许使用索引运算符在集合中存储值。

IntIterator SparseIntArray.valueIterator()

针对该集合的值返回迭代器。

对于 android.util.SparseLongArray
运算符 Boolean SparseLongArray.contains(key: Int)

如果该集合包含 key,则返回 true。

Boolean SparseLongArray.containsKey(key: Int)

如果该集合包含 key,则返回 true。

Boolean SparseLongArray.containsValue(value: Long)

如果该集合包含 value,则返回 true。

Unit SparseLongArray.forEach(action: (key: Int, value: Long) -> Unit)

针对各个键值对条目执行指定的操作

Long SparseLongArray.getOrDefault(key: Int, defaultValue: Long)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue

Long SparseLongArray.getOrElse(key: Int, defaultValue: () -> Long)

返回与 key 对应的值,如果该值不存在,则返回 defaultValue 中的值。

Boolean SparseLongArray.isEmpty()

当集合中未包含任何元素时,返回 true。

Boolean SparseLongArray.isNotEmpty()

当集合中包含多个元素时,返回 true。

IntIterator SparseLongArray.keyIterator()

针对该集合的键返回迭代器。

运算符 SparseLongArray SparseLongArray.plus(other: SparseLongArray)

通过添加或替换 other 中的条目来创建一个新集合。

Unit SparseLongArray.putAll(other: SparseLongArray)

通过添加或替换 other 中的条目来更新此集合。

Boolean SparseLongArray.remove(key: Int, value: Long)

仅当 key 的条目设置为 value 时,才移除该条目。

运算符 Unit SparseLongArray.set(key: Int, value: Long)

允许使用索引运算符在集合中存储值。

LongIterator SparseLongArray.valueIterator()

针对该集合的值返回迭代器。

对于 kotlin.Double
Half Double.toHalf()

返回表示指定 DoubleHalf 实例。

对于 kotlin.Float
Half Float.toHalf()

返回表示指定浮点数Half 实例。

对于 kotlin.Pair
Pair<F, S> Pair<F, S>.toAndroidPair()

返回此 kotlin.Pair 作为 Android 键值对

对于 kotlin.Short
Half Short.toHalf()

返回表示指定 ShortHalf 实例。

对于 kotlin.String
Half String.toHalf()

返回表示指定字符串Half 实例。

对于 kotlin.ranges.ClosedRange
Range<T> ClosedRange<T>.toRange()

将该 ClosedRange 作为 Rang 返回。

扩展属性

对于 android.util.LongSparseArray
Int LongSparseArray<T>.size()

返回集合中键/值对的数量。

对于 android.util.SparseArray
Int SparseArray<T>.size()

返回集合中键/值对的数量。

对于 android.util.SparseBooleanArray
Int SparseBooleanArray.size()

返回集合中键/值对的数量。

对于 android.util.SparseIntArray
Int SparseIntArray.size()

返回集合中键/值对的数量。

对于 android.util.SparseLongArray
Int SparseLongArray.size()

返回集合中键/值对条目的数量。

顶级函数

LruCache<K, V> lruCache(maxSize: Int, crossinline sizeOf: (key: K, value: V) -> Int = { _, _ -> 1 }, crossinline create: (key: K) -> V? = { null as V? }, crossinline onEntryRemoved: (evicted: Boolean, key: K, oldValue: V, newValue: V?) -> Unit = { _, _, _, _ -> })

创建具有指定参数的 LruCache

infix Range<T> T.rangeTo(that: T)

创建一个介于此可比较值和值之间的范围。

androidx.core.view

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.view.Menu
运算符 Boolean Menu.contains(item: MenuItem)

如果在此菜单中找到,则返回true

Unit Menu.forEach(action: (item: MenuItem) -> Unit)

针对此菜单中的每个项执行指定的操作。

Unit Menu.forEachIndexed(action: (index: Int, item: MenuItem) -> Unit)

针对此菜单中的每个项执行指定的操作,并提供其顺序索引。

运算符 MenuItem Menu.get(index: Int)

返回索引处的菜单。

Boolean Menu.isEmpty()

如果此菜单中未包含任何项,则返回 true。

Boolean Menu.isNotEmpty()

如果此菜单中包含一个或多个项,则返回 true。

运算符 MutableIterator<MenuItem> Menu.iterator()

针对此菜单中的项返回 MutableIterator

运算符 Unit Menu.minusAssign(item: MenuItem)

从此菜单中移除

对于 android.view.View
Unit View.doOnAttach(crossinline action: (view: View) -> Unit)

当该视图附加到窗口时,执行指定的操作。

Unit View.doOnDetach(crossinline action: (view: View) -> Unit)

当该视图与窗口分离时,执行指定的操作。

Unit View.doOnLayout(crossinline action: (view: View) -> Unit)

布局该视图时,执行指定的操作。

Unit View.doOnNextLayout(crossinline action: (view: View) -> Unit)

当再次布局该视图时,执行指定的操作。

OneShotPreDrawListener View.doOnPreDraw(crossinline action: (view: View) -> Unit)

当要绘制视图树时,执行指定的操作。

Bitmap View.drawToBitmap(config: Config = Bitmap.Config.ARGB_8888)

返回该视图位图表示法。

Runnable View.postDelayed(delayInMillis: Long, crossinline action: () -> Unit)

View.postDelayed 的一个版本,用于对参数重新排序,允许将操作置于括号之外。

Runnable View.postOnAnimationDelayed(delayInMillis: Long, crossinline action: () -> Unit)

View.postOnAnimationDelayed 的一个版本,用于对参数重新排序,允许将操作置于括号之外。

Unit View.setPadding(@Px size: Int)

设置视图的内边距。

Unit View.updateLayoutParams(block: LayoutParams.() -> Unit)

使用视图的 layoutParams 执行 block,并使用更新版本重新分配 layoutParams。

Unit View.updateLayoutParams(block: T.() -> Unit)

使用视图 layoutParams 的打印版本执行 block,并使用更新版本重新分配 layoutParams。

Unit View.updatePadding(@Px left: Int = paddingLeft, @Px top: Int = paddingTop, @Px right: Int = paddingRight, @Px bottom: Int = paddingBottom)

更新该视图的内边距。

Unit View.updatePaddingRelative(@Px start: Int = paddingStart, @Px top: Int = paddingTop, @Px end: Int = paddingEnd, @Px bottom: Int = paddingBottom)

更新该视图的相对内边距。

对于 android.view.ViewGroup
运算符 Boolean ViewGroup.contains(view: View)

如果在该视图组中发现视图,则返回 true

Unit ViewGroup.forEach(action: (view: View) -> Unit)

对该视图组中的各个视图执行指定的操作。

Unit ViewGroup.forEachIndexed(action: (index: Int, view: View) -> Unit)

对该视图组中的各个视图执行指定的操作,并提供其顺序索引。

运算符 View ViewGroup.get(index: Int)

返回索引处的视图。

Boolean ViewGroup.isEmpty()

如果该视图组不包含任何视图,则返回 true。

Boolean ViewGroup.isNotEmpty()

如果该视图组包含一个或多个视图,则返回 true。

运算符 MutableIterator<View> ViewGroup.iterator()

针对该视图组中的视图返回 MutableIterator

运算符 Unit ViewGroup.minusAssign(view: View)

从该视图组中移除视图

运算符 Unit ViewGroup.plusAssign(view: View)

向该视图组添加视图

对于 android.view.ViewGroup.MarginLayoutParams
Unit MarginLayoutParams.setMargins(@Px size: Int)

在 ViewGroup 的 MarginLayoutParams 中设置外边距。

Unit MarginLayoutParams.updateMargins(@Px left: Int = leftMargin, @Px top: Int = topMargin, @Px right: Int = rightMargin, @Px bottom: Int = bottomMargin)

ViewGroupViewGroup.MarginLayoutParams 中更新外边距。

Unit MarginLayoutParams.updateMarginsRelative(@Px start: Int = marginStart, @Px top: Int = topMargin, @Px end: Int = marginEnd, @Px bottom: Int = bottomMargin)

在 ViewGroup 的 MarginLayoutParams 中更新相对外边距。

扩展属性

对于 android.view.Menu
Sequence<MenuItem> Menu.children()

针对此菜单中的项返回一个序列

Int Menu.size()

返回此菜单中的项数。

对于 android.view.View
Boolean View.isGone()

当该视图的可见性为 View.GONE 时,返回 true,否则返回 false。

Boolean View.isInvisible()

当该视图的可见性为 View.INVISIBLE 时,返回 true,否则返回 false。

Boolean View.isVisible()

当该视图的可见性为 View.VISIBLE 时,返回 true,否则返回 false。

Int View.marginBottom()

如果该视图的 LayoutParamsViewGroup.MarginLayoutParams,则返回下外边距,否则返回 0。

Int View.marginEnd()

如果该视图的 LayoutParamsViewGroup.MarginLayoutParams,则返回末端外边距,否则返回 0。

Int View.marginLeft()

如果该视图的 LayoutParamsViewGroup.MarginLayoutParams,则返回左外边距,否则返回 0。

Int View.marginRight()

如果该视图的 LayoutParamsViewGroup.MarginLayoutParams,则返回右外边距,否则返回 0。

Int View.marginStart()

如果该视图的 LayoutParamsViewGroup.MarginLayoutParams,则返回起始外边距,否则返回 0。

Int View.marginTop()

如果该视图的 LayoutParamsViewGroup.MarginLayoutParams,则返回上外边距,否则返回 0。

对于 android.view.ViewGroup
Sequence<View> ViewGroup.children()

针对该视图组中的子视图返回一个序列

Int ViewGroup.size()

返回该视图组中的视图数。

androidx.core.widget

依赖项

Groovy

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}

扩展函数

对于 android.widget.TextView
TextWatcher TextView.addTextChangedListener(crossinline beforeTextChanged: (text: CharSequence?, start: Int, count: Int, after: Int) -> Unit = { _, _, _, _ -> }, crossinline onTextChanged: (text: CharSequence?, start: Int, count: Int, after: Int) -> Unit = { _, _, _, _ -> }, crossinline afterTextChanged: (text: Editable?) -> Unit = {})

使用提供的操作为此 TextView 添加文本更改监听器

TextWatcher TextView.doAfterTextChanged(crossinline action: (text: Editable?) -> Unit)

添加将在文本更改后调用的操作。

TextWatcher TextView.doBeforeTextChanged(crossinline action: (text: CharSequence?, start: Int, count: Int, after: Int) -> Unit)

添加将在文本更改之前调用的操作。

TextWatcher TextView.doOnTextChanged(crossinline action: (text: CharSequence?, start: Int, count: Int, after: Int) -> Unit)

添加将在文本更改时调用的操作。

androidx.dynamicanimation.animation

依赖项

Groovy

dependencies {
    implementation "androidx.dynamicanimation:dynamicanimation-ktx:1.1.0-alpha03"
}

Kotlin

dependencies {
    implementation("androidx.dynamicanimation:dynamicanimation-ktx:1.1.0-alpha03")
}

扩展函数

对于 SpringAnimation
SpringAnimation SpringAnimation.withSpringForceProperties(func: SpringForce.() -> Unit)

在 SpringAnimation 上更新或应用 SpringForce.mDampingRatioSpringForce.mFinalPosition 和刚度等弹簧弹力属性。

顶级函数

FlingAnimation flingAnimationOf(setter: (Float) -> Unit, getter: () -> Float)

针对可通过提供的 setter 和 getter 访问的属性创建 FlingAnimation

SpringAnimation springAnimationOf(setter: (Float) -> Unit, getter: () -> Float, finalPosition: Float = Float.NaN)

针对可通过提供的 setter 和 getter 访问的属性创建 SpringAnimation

androidx.fragment.app

依赖项

Groovy

dependencies {
    implementation "androidx.fragment:fragment-ktx:1.6.2"
}

Kotlin

dependencies {
    implementation("androidx.fragment:fragment-ktx:1.6.2")
}

扩展函数

对于 android.view.View
F View.findFragment()

查找与视图相关联的 Fragment

对于 Fragment
Lazy<VM> Fragment.activityViewModels(noinline factoryProducer: () -> ViewModelProvider.Factory = null)

返回一个属性委托以访问父级 Activity 的 ViewModel。如果已指定 factoryProducer,则它返回的 ViewModelProvider.Factory 将用于首次创建 ViewModel

Lazy<VM> Fragment.createViewModelLazy(viewModelClass: KClass<VM>, storeProducer: () -> ViewModelStore, factoryProducer: () -> ViewModelProvider.Factory = null)

用于创建 ViewModelLazy 的辅助程序方法,会将作为 factoryProducer 传递的 null 解析为默认出厂设置。

Lazy<VM> Fragment.viewModels(noinline ownerProducer: () -> ViewModelStoreOwner = { this }, noinline factoryProducer: () -> ViewModelProvider.Factory = null)

返回一个属性委托,以便访问默认将范围限定为此 FragmentViewModel

对于 FragmentTransaction
FragmentTransaction FragmentTransaction.add(@IdRes containerViewId: Int, tag: String? = null, args: Bundle? = null)

将 Fragment 添加到关联的 FragmentManager 中,同时将 Fragment 的视图扩充到由 containerViewId 指定的容器视图中,以便稍后通过 FragmentManager.findFragmentById 进行检索。

FragmentTransaction FragmentTransaction.add(tag: String, args: Bundle? = null)

将 Fragment 添加到关联的 FragmentManager 中,而无需将 Fragment 添加到任何容器视图中。

FragmentTransaction FragmentTransaction.replace(@IdRes containerViewId: Int, tag: String? = null, args: Bundle? = null)

替换添加到容器中的现有 Fragment。

对于 FragmentManager
Unit FragmentManager.commit(allowStateLoss: Boolean = false, body: FragmentTransaction.() -> Unit)

FragmentTransaction 中运行 body,如果顺利完成,则系统将自动提交该对象。

Unit FragmentManager.commitNow(allowStateLoss: Boolean = false, body: FragmentTransaction.() -> Unit)

FragmentTransaction 中运行 body,如果顺利完成,则系统将自动提交该对象。

Unit FragmentManager.transaction(now: Boolean = false, allowStateLoss: Boolean = false, body: FragmentTransaction.() -> Unit)

FragmentTransaction 中运行 body,如果顺利完成,则系统将自动提交该对象。

androidx.fragment.app.testing

依赖项

Groovy

dependencies {
    implementation "androidx.fragment:fragment-testing:1.6.2"
}

Kotlin

dependencies {
    implementation("androidx.fragment:fragment-testing:1.6.2")
}

顶级函数

FragmentScenario<F!> launchFragment(fragmentArgs: Bundle? = null, @StyleRes themeResId: Int = R.style.FragmentScenarioEmptyFragmentActivityTheme, factory: FragmentFactory? = null)

使用指定的 FragmentFactory 启动具有由空 FragmentActivity 托管的指定参数的 Fragment,并等待其达到“已恢复”状态。

FragmentScenario<F!> launchFragment(fragmentArgs: Bundle? = null, @StyleRes themeResId: Int = R.style.FragmentScenarioEmptyFragmentActivityTheme, crossinline instantiate: () -> F)

使用实例化启动具有由空 FragmentActivity 托管的指定参数的 Fragment,以创建 Fragment,并等待其达到“已恢复”状态。

FragmentScenario<F!> launchFragmentInContainer(fragmentArgs: Bundle? = null, @StyleRes themeResId: Int = R.style.FragmentScenarioEmptyFragmentActivityTheme, factory: FragmentFactory? = null)

在 Activity 的根视图容器 android.R.id.content 中启动具有由空 FragmentActivity 托管的指定参数的 Fragment,并等待其达到“已恢复”状态。

FragmentScenario<F!> launchFragmentInContainer(fragmentArgs: Bundle? = null, @StyleRes themeResId: Int = R.style.FragmentScenarioEmptyFragmentActivityTheme, crossinline instantiate: () -> F)

使用实例化在 Activity 的根视图容器 android.R.id.content 启动具有由空 FragmentActivity 托管的指定参数的 Fragment,以创建 Fragment,并等待其达到“已恢复”状态。

androidx.lifecycle

依赖项

Groovy

dependencies {
    implementation "androidx.lifecycle:lifecycle-livedata-core-ktx:2.6.2"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.6.2"
    implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:2.6.2"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.2"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
}

Kotlin

dependencies {
    implementation("androidx.lifecycle:lifecycle-livedata-core-ktx:2.6.2")
    implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.2")
    implementation("androidx.lifecycle:lifecycle-reactivestreams-ktx:2.6.2")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2")
}

扩展函数

对于 kotlinx.coroutines.flow.Flow
LiveData<T> Flow<T>.asLiveData(context: CoroutineContext = EmptyCoroutineContext, timeoutInMs: Long = DEFAULT_TIMEOUT)

创建一个 LiveData,其中包含从原始收集的值。

LiveData<T> Flow<T>.asLiveData(context: CoroutineContext = EmptyCoroutineContext, timeout: Duration)

创建一个 LiveData,其中包含从原始收集的值。

对于 org.reactivestreams.Publisher
LiveData<T> Publisher<T>.toLiveData()

从 ReactiveStreams 发布商处创建可观察的 LiveData 数据流。

对于 LiveData
Flow<T> LiveData<T>.asFlow()

创建一个。其中包含由原始 LiveData 分派的值:首先,流收集器接收由 LiveData 持有的最新值,然后观察 LiveData 更新。

LiveData<X> LiveData<X>.distinctUntilChanged()

创建一个新的 LiveData 对象,该对象在源 this LiveData 值发生更改之前不会发出值。

LiveData<Y> LiveData<X>.map(crossinline transform: (X) -> Y)

转换应用于在 this LiveData 上设置的各个值,从而返回从 this LiveData 映射的 LiveData

Observer<T> LiveData<T>.observe(owner: LifecycleOwner, crossinline onChanged: (T) -> Unit)

在指定的所有者的生命周期内,添加指定的 onChanged lambda 作为观察者,并返回对观察者的引用。

LiveData<Y> LiveData<X>.switchMap(crossinline transform: (X) -> LiveData<Y>)

转换应用于在 this 上设置的各个值,从而返回从输入 this LiveData 映射的 LiveData

Publisher<T> LiveData<T>.toPublisher(lifecycle: LifecycleOwner)

将指定的 LiveData 数据流调整为 ReactiveStreams 发布商

对于 ViewModelProvider
VM ViewModelProvider.get()

返回现有的 ViewModel,或者在相应范围(通常为 Fragment 或 Activity)内创建一个与此 ViewModelProvider 关联的新 ViewModel。

对于 LifecycleOwner
suspend T LifecycleOwner.whenCreated(block: suspend CoroutineScope.() -> T)

LifecycleOwner生命周期至少处于 Lifecycle.State.CREATED 状态时,运行指定的 block。

suspend T LifecycleOwner.whenResumed(block: suspend CoroutineScope.() -> T)

LifecycleOwner生命周期至少处于 Lifecycle.State.RESUMED 状态时,运行指定的 block。

suspend T LifecycleOwner.whenStarted(block: suspend CoroutineScope.() -> T)

LifecycleOwner生命周期至少处于 Lifecycle.State.STARTED 状态时,运行指定的 block。

对于 Lifecycle
suspend T Lifecycle.whenCreated(block: suspend CoroutineScope.() -> T)

生命周期至少处于 Lifecycle.State.CREATED 状态时,运行指定的 block。

suspend T Lifecycle.whenResumed(block: suspend CoroutineScope.() -> T)

生命周期至少处于 Lifecycle.State.RESUMED 状态时,运行指定的 block。

suspend T Lifecycle.whenStarted(block: suspend CoroutineScope.() -> T)

生命周期至少处于 Lifecycle.State.STARTED 状态时,运行指定的 block。

suspend T Lifecycle.whenStateAtLeast(minState: Lifecycle.State, block: suspend CoroutineScope.() -> T)

在能够在主线程上执行相应 blockCoroutineDispatcher 上运行指定的 block,并且仅当生命周期的状态至少处于 minState 时才执行该 block。

扩展属性

对于 Lifecycle
LifecycleCoroutineScope Lifecycle.coroutineScope()

CoroutineScope 与该生命周期相关联。

对于 LifecycleOwner
LifecycleCoroutineScope LifecycleOwner.lifecycleScope()

CoroutineScope 与该 LifecycleOwner生命周期相关联。

对于 ViewModel
CoroutineScope ViewModel.viewModelScope()

CoroutineScope 与该 ViewModel 相关联。

顶级函数

LiveData<T> liveData(context: CoroutineContext = EmptyCoroutineContext, timeoutInMs: Long = DEFAULT_TIMEOUT, block: suspend LiveDataScope<T>.() -> Unit)

构建一个 LiveData,其中包含从在 LiveDataScope 上执行的指定 block 中获取的值。

LiveData<T> liveData(context: CoroutineContext = EmptyCoroutineContext, timeout: Duration, block: suspend LiveDataScope<T>.() -> Unit)

构建一个 LiveData,其中包含从在 LiveDataScope 上执行的指定 block 中获取的值。

androidx.navigation

依赖项

Groovy

dependencies {
    implementation "androidx.navigation:navigation-runtime-ktx:2.7.5"
    implementation "androidx.navigation:navigation-fragment-ktx:2.7.5"
    implementation "androidx.navigation:navigation-ui-ktx:2.7.5"
}

Kotlin

dependencies {
    implementation("androidx.navigation:navigation-runtime-ktx:2.7.5")
    implementation("androidx.navigation:navigation-fragment-ktx:2.7.5")
    implementation("androidx.navigation:navigation-ui-ktx:2.7.5")
}

扩展函数

对于 android.app.Activity
NavController Activity.findNavController(@IdRes viewId: Int)

根据视图 ID 及其包含的 Activity 查找 NavController

NavArgsLazy<Args> Activity.navArgs()

返回一个延迟委托,以便访问 Activity 的额外项作为 Args 实例。

对于 android.view.View
NavController View.findNavController()

查找与视图相关联的 NavController

对于 NavGraphBuilder
Unit NavGraphBuilder.activity(@IdRes id: Int, builder: ActivityNavigatorDestinationBuilder.() -> Unit)

构造一个新的 ActivityNavigator.Destination

Unit NavGraphBuilder.navigation(@IdRes id: Int, @IdRes startDestination: Int, builder: NavGraphBuilder.() -> Unit)

构造一个嵌套的 NavGraph

对于 NavGraph
运算符 Boolean NavGraph.contains(@IdRes id: Int)

如果在该导航图中找到了具有 id 的目的地,则返回 true

运算符 NavDestination NavGraph.get(@IdRes id: Int)

返回具有 id 的目的地。

运算符 Unit NavGraph.minusAssign(node: NavDestination)

从该导航图中移除 node

运算符 Unit NavGraph.plusAssign(node: NavDestination)

向该 NavGraph 添加目的地。

运算符 Unit NavGraph.plusAssign(other: NavGraph)

将其他集合中的所有目的地均添加到该集合中。

对于 NavController
NavGraph NavController.createGraph(@IdRes id: Int = 0, @IdRes startDestination: Int, builder: NavGraphBuilder.() -> Unit)

构造一个新的 NavGraph

对于 NavHost
NavGraph NavHost.createGraph(@IdRes id: Int = 0, @IdRes startDestination: Int, builder: NavGraphBuilder.() -> Unit)

构造一个新的 NavGraph

对于 NavigatorProvider
运算符 T NavigatorProvider.get(name: String)

按名称检索已注册的导航器

运算符 T NavigatorProvider.get(clazz: KClass<T>)

使用由 Navigator.Name annotation 提供的名称检索已注册的导航器

NavGraph NavigatorProvider.navigation(@IdRes id: Int = 0, @IdRes startDestination: Int, builder: NavGraphBuilder.() -> Unit)

构造一个新的 NavGraph

运算符 Unit NavigatorProvider.plusAssign(navigator: Navigator<out NavDestination>)

使用由 Navigator.Name annotation 提供的名称注册导航器。

运算符 Navigator<out NavDestination!>? NavigatorProvider.set(name: String, navigator: Navigator<out NavDestination>)

按名称注册导航器

对于 Fragment
Lazy<VM> Fragment.navGraphViewModels(@IdRes navGraphId: Int, noinline factoryProducer: () -> ViewModelProvider.Factory = null)

返回一个属性委托,以便访问范围限定为在 {@link NavController} 返回堆栈上显示的导航图的 ViewModel

顶级函数

ActivityNavigator.Extras ActivityNavigatorExtras(activityOptions: ActivityOptionsCompat? = null, flags: Int = 0)

使用特定的 ActivityOptionsCompat 实例和/或任何 Intent.FLAG_ACTIVITY_ 标记创建新的 ActivityNavigator.Extras 实例。

NavOptions navOptions(optionsBuilder: NavOptionsBuilder.() -> Unit)

构造一个新的 NavOptions

androidx.navigation.fragment

依赖项

Groovy

dependencies {
    implementation "androidx.navigation:navigation-fragment-ktx:2.7.5"
}

Kotlin

dependencies {
    implementation("androidx.navigation:navigation-fragment-ktx:2.7.5")
}

扩展函数

对于 NavGraphBuilder
Unit NavGraphBuilder.dialog(@IdRes id: Int)

构造一个新的 DialogFragmentNavigator.Destination

Unit NavGraphBuilder.dialog(@IdRes id: Int, builder: DialogFragmentNavigatorDestinationBuilder.() -> Unit)

构造一个新的 DialogFragmentNavigator.Destination

Unit NavGraphBuilder.fragment(@IdRes id: Int)

构造一个新的 FragmentNavigator.Destination

Unit NavGraphBuilder.fragment(@IdRes id: Int, builder: FragmentNavigatorDestinationBuilder.() -> Unit)

构造一个新的 FragmentNavigator.Destination

对于 Fragment
NavController Fragment.findNavController()

根据 Fragment 查找 NavController

NavArgsLazy<Args> Fragment.navArgs()

返回一个延迟委托,以便访问 Fragment 的参数作为 Args 实例。

顶级函数

FragmentNavigator.Extras FragmentNavigatorExtras(vararg sharedElements: Pair<View, String>)

使用指定的共享元素创建新的 FragmentNavigator.Extras 实例

androidx.navigation.ui

依赖项

Groovy

dependencies {
    implementation "androidx.navigation:navigation-ui-ktx:2.7.5"
}

Kotlin

dependencies {
    implementation("androidx.navigation:navigation-ui-ktx:2.7.5")
}

扩展函数

对于 android.view.MenuItem
Boolean MenuItem.onNavDestinationSelected(navController: NavController)

尝试转到与该 MenuItem 相关联的 NavDestination

对于 androidx.appcompat.app.AppCompatActivity
Unit AppCompatActivity.setupActionBarWithNavController(navController: NavController, drawerLayout: DrawerLayout?)

设置由 AppCompatActivity.getSupportActionBar 返回的 ActionBar,以便与 NavController 配合使用。

Unit AppCompatActivity.setupActionBarWithNavController(navController: NavController, configuration: AppBarConfiguration = AppBarConfiguration(navController.graph))

设置由 AppCompatActivity.getSupportActionBar 返回的 ActionBar,以便与 NavController 配合使用。

对于 androidx.appcompat.widget.Toolbar
Unit Toolbar.setupWithNavController(navController: NavController, drawerLayout: DrawerLayout?)

设置工具栏,以便与 NavController 配合使用。

Unit Toolbar.setupWithNavController(navController: NavController, configuration: AppBarConfiguration = AppBarConfiguration(navController.graph))

设置工具栏,以便与 NavController 配合使用。

对于 com.google.android.material.appbar.CollapsingToolbarLayout
Unit CollapsingToolbarLayout.setupWithNavController(toolbar: Toolbar, navController: NavController, drawerLayout: DrawerLayout?)

设置 CollapsingToolbarLayout工具栏,以便与 NavController 配合使用。

Unit CollapsingToolbarLayout.setupWithNavController(toolbar: Toolbar, navController: NavController, configuration: AppBarConfiguration = AppBarConfiguration(navController.graph))

设置 CollapsingToolbarLayout工具栏,以便与 NavController 配合使用。

对于 com.google.android.material.bottomnavigation.BottomNavigationView
Unit BottomNavigationView.setupWithNavController(navController: NavController)

设置 BottomNavigationView,以便与 NavController 配合使用。

对于 com.google.android.material.navigation.NavigationView
Unit NavigationView.setupWithNavController(navController: NavController)

设置 NavigationView,以便与 NavController 配合使用。

对于 NavController
Boolean NavController.navigateUp(drawerLayout: DrawerLayout?)

通过将“向上”按钮的行为委托给指定的 NavController 来处理该按钮。

Boolean NavController.navigateUp(appBarConfiguration: AppBarConfiguration)

通过将“向上”按钮的行为委托给指定的 NavController 来处理该按钮。

顶级函数

AppBarConfiguration AppBarConfiguration(navGraph: NavGraph, drawerLayout: DrawerLayout? = null, noinline fallbackOnNavigateUpListener: () -> Boolean = { false })

与应用栏模式的实现进行互动的 NavigationUI 方法的配置选项,例如 android.support.v7.widget.Toolbarandroid.support.design.widget .CollapsingToolbarLayoutandroid.support.v7.app.ActionBar

AppBarConfiguration AppBarConfiguration(topLevelMenu: Menu, drawerLayout: DrawerLayout? = null, noinline fallbackOnNavigateUpListener: () -> Boolean = { false })

与应用栏模式的实现进行互动的 NavigationUI 方法的配置选项,例如 android.support.v7.widget.Toolbarandroid.support.design.widget .CollapsingToolbarLayoutandroid.support.v7.app.ActionBar

AppBarConfiguration AppBarConfiguration(topLevelDestinationIds: Set<Int>, drawerLayout: DrawerLayout? = null, noinline fallbackOnNavigateUpListener: () -> Boolean = { false })

与应用栏模式的实现进行互动的 NavigationUI 方法的配置选项,例如 android.support.v7.widget.Toolbarandroid.support.design.widget .CollapsingToolbarLayoutandroid.support.v7.app.ActionBar

androidx.paging

依赖项

Groovy

dependencies {
    implementation "androidx.paging:paging-common-ktx:2.1.2"
    implementation "androidx.paging:paging-runtime-ktx:2.1.2"
    implementation "androidx.paging:paging-rxjava2-ktx:2.1.2"
}

Kotlin

dependencies {
    implementation("androidx.paging:paging-common-ktx:2.1.2")
    implementation("androidx.paging:paging-runtime-ktx:2.1.2")
    implementation("androidx.paging:paging-rxjava2-ktx:2.1.2")
}

扩展函数

对于 Factory
Flowable<PagedList<Value>> DataSource.Factory<Key, Value>.toFlowable(config: PagedList.Config, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchScheduler: Scheduler? = null, notifyScheduler: Scheduler? = null, backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST)

从该 DataSource.Factory 构造一个 Flowable<PagedList>,从而为 RxPagedListBuilder 提供便利。

Flowable<PagedList<Value>> DataSource.Factory<Key, Value>.toFlowable(pageSize: Int, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchScheduler: Scheduler? = null, notifyScheduler: Scheduler? = null, backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST)

从该 DataSource.Factory 构造一个 Flowable<PagedList>,从而为 RxPagedListBuilder 提供便利。

LiveData<PagedList<Value>> DataSource.Factory<Key, Value>.toLiveData(config: PagedList.Config, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchExecutor: Executor = ArchTaskExecutor.getIOThreadExecutor())

从该 DataSource.Factory 构造一个 LiveData<PagedList>,从而为 LivePagedListBuilder 提供便利。

LiveData<PagedList<Value>> DataSource.Factory<Key, Value>.toLiveData(pageSize: Int, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchExecutor: Executor = ArchTaskExecutor.getIOThreadExecutor())

从该 DataSource.Factory 构造一个 LiveData<PagedList>,从而为 LivePagedListBuilder 提供便利。

Observable<PagedList<Value>> DataSource.Factory<Key, Value>.toObservable(config: PagedList.Config, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchScheduler: Scheduler? = null, notifyScheduler: Scheduler? = null)

从该 DataSource.Factory 构造一个 Observable<PagedList>,从而为 RxPagedListBuilder 提供便利。

Observable<PagedList<Value>> DataSource.Factory<Key, Value>.toObservable(pageSize: Int, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchScheduler: Scheduler? = null, notifyScheduler: Scheduler? = null)

从该 DataSource.Factory 构造一个 Observable<PagedList>,从而为 RxPagedListBuilder 提供便利。

顶级函数

PagedList.Config Config(pageSize: Int, prefetchDistance: Int = pageSize, enablePlaceholders: Boolean = true, initialLoadSizeHint: Int = pageSize * PagedList.Config.Builder.DEFAULT_INITIAL_PAGE_MULTIPLIER, maxSize: Int = PagedList.Config.MAX_SIZE_UNBOUNDED)

构造一个 PagedList.Config,从而为 PagedList.Config.Builder 提供便利。

PagedList<Value> PagedList(dataSource: DataSource<Key, Value>, config: PagedList.Config, notifyExecutor: Executor, fetchExecutor: Executor, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, initialKey: Key? = null)

构造一个 PagedList,从而为 PagedList.Builder 提供便利。

androidx.palette.graphics

依赖项

Groovy

dependencies {
    implementation "androidx.palette:palette-ktx:1.0.0"
}

Kotlin

dependencies {
    implementation("androidx.palette:palette-ktx:1.0.0")
}

扩展函数

对于 Palette
运算符 Palette.Swatch? Palette.get(target: Target)

从调色板返回指定目标的选定色样,如果未找到,则返回 null

androidx.preference

依赖项

Groovy

dependencies {
    implementation "androidx.preference:preference-ktx:1.2.1"
}

Kotlin

dependencies {
    implementation("androidx.preference:preference-ktx:1.2.1")
}

扩展函数

对于 PreferenceGroup
运算符 Boolean PreferenceGroup.contains(preference: Preference)

如果在该偏好设置组中找到 preference,则返回 true

Unit PreferenceGroup.forEach(action: (preference: Preference) -> Unit)

针对该偏好设置组中的每个偏好设置执行指定的操作。

Unit PreferenceGroup.forEachIndexed(action: (index: Int, preference: Preference) -> Unit)

针对该偏好设置组中的每个偏好设置执行指定的操作,并提供其顺序索引。

运算符 T? PreferenceGroup.get(key: CharSequence)

返回具有 key 的偏好设置,或者如果未找到具有 key 的偏好设置,则返回 null

运算符 Preference PreferenceGroup.get(index: Int)

返回 index 处的偏好设置。

Boolean PreferenceGroup.isEmpty()

如果该偏好设置组中未包含任何偏好设置,则返回 true。

Boolean PreferenceGroup.isNotEmpty()

如果该偏好设置组中包含一个或多个偏好设置,则返回 true。

运算符 MutableIterator<Preference> PreferenceGroup.iterator()

针对该偏好设置组中的偏好设置返回 MutableIterator

运算符 Unit PreferenceGroup.minusAssign(preference: Preference)

从该偏好设置组中移除 preference

运算符 Unit PreferenceGroup.plusAssign(preference: Preference)

向该偏好设置组中添加 preference

扩展属性

对于 PreferenceGroup
Sequence<Preference> PreferenceGroup.children()

针对该偏好设置组中的偏好设置返回序列

Int PreferenceGroup.size()

返回该偏好设置组中的偏好设置数量。

androidx.room

依赖项

Groovy

dependencies {
    implementation "androidx.room:room-ktx:2.6.1"
}

Kotlin

dependencies {
    implementation("androidx.room:room-ktx:2.6.1")
}

扩展函数

对于 RoomDatabase
suspend R RoomDatabase.withTransaction(block: suspend () -> R)

在数据库事务中调用指定的挂起 block

androidx.slice.builders

依赖项

Groovy

dependencies {
    implementation "androidx.slice:slice-builders-ktx:1.0.0-alpha08"
}

Kotlin

dependencies {
    implementation("androidx.slice:slice-builders-ktx:1.0.0-alpha08")
}

扩展函数

对于 GridRowBuilderDsl
GridRowBuilder GridRowBuilderDsl.cell(buildCell: CellBuilderDsl.() -> Unit)

GridRowBuilder GridRowBuilderDsl.seeMoreCell(buildCell: CellBuilderDsl.() -> Unit)

对于 ListBuilderDsl
ListBuilder ListBuilderDsl.gridRow(buildGrid: GridRowBuilderDsl.() -> Unit)

ListBuilder ListBuilderDsl.header(buildHeader: HeaderBuilderDsl.() -> Unit)

ListBuilder ListBuilderDsl.inputRange(buildInputRange: InputRangeBuilderDsl.() -> Unit)

ListBuilder ListBuilderDsl.range(buildRange: RangeBuilderDsl.() -> Unit)

ListBuilder ListBuilderDsl.row(buildRow: RowBuilderDsl.() -> Unit)

ListBuilder ListBuilderDsl.seeMoreRow(buildRow: RowBuilderDsl.() -> Unit)

顶级函数

Slice list(context: Context, uri: Uri, ttl: Long, addRows: ListBuilderDsl.() -> Unit)

降低在 Kotlin 中构建切片所需的详细程度。

SliceAction tapSliceAction(pendingIntent: PendingIntent, icon: IconCompat, imageMode: Int = ICON_IMAGE, title: CharSequence)

用于构建可点按的 SliceAction 的工厂方法。

SliceAction toggleSliceAction(pendingIntent: PendingIntent, icon: IconCompat? = null, title: CharSequence, isChecked: Boolean)

用于构建可切换的 SliceAction 的工厂方法。

androidx.sqlite.db

依赖项

Groovy

dependencies {
    implementation "androidx.sqlite:sqlite-ktx:2.4.0"
}

Kotlin

dependencies {
    implementation("androidx.sqlite:sqlite-ktx:2.4.0")
}

扩展函数

对于 SupportSQLiteDatabase
T SupportSQLiteDatabase.transaction(exclusive: Boolean = true, body: SupportSQLiteDatabase.() -> T)

在事务中运行 body,如果顺利完成,则将其标记为“成功”。

androidx.work

依赖项

扩展函数

对于 com.google.common.util.concurrent.ListenableFuture
suspend R ListenableFuture<R>.await()

等待 ListenableFuture 完成,而无需屏蔽线程。

对于 Operation
suspend Operation.State.SUCCESS! Operation.await()

等待操作,而无需屏蔽线程。

对于 Data
Boolean Data.hasKeyWithValueOfType(key: String)

如果数据的实例具有与带有预期类型 T 的指定 key 对应的值,则返回 true。

对于 Builder
OneTimeWorkRequest.Builder OneTimeWorkRequest.Builder.setInputMerger(@NonNull inputMerger: KClass<out InputMerger>)

OneTimeWorkRequest.Builder 上设置 InputMerger

顶级函数

OneTimeWorkRequest.Builder OneTimeWorkRequestBuilder()

使用指定的 ListenableWorker 创建一个 OneTimeWorkRequest

PeriodicWorkRequest.Builder PeriodicWorkRequestBuilder(repeatInterval: Long, repeatIntervalTimeUnit: TimeUnit)

使用指定的 ListenableWorker 创建一个 PeriodicWorkRequest.Builder

PeriodicWorkRequest.Builder PeriodicWorkRequestBuilder(repeatInterval: Duration)

使用指定的 ListenableWorker 创建一个 PeriodicWorkRequest.Builder

PeriodicWorkRequest.Builder PeriodicWorkRequestBuilder(repeatInterval: Long, repeatIntervalTimeUnit: TimeUnit, flexTimeInterval: Long, flexTimeIntervalUnit: TimeUnit)

使用指定的 ListenableWorker 创建一个 PeriodicWorkRequest.Builder

PeriodicWorkRequest.Builder PeriodicWorkRequestBuilder(repeatInterval: Duration, flexTimeInterval: Duration)

使用指定的 ListenableWorker 创建一个 PeriodicWorkRequest.Builder

Data workDataOf(vararg pairs: Pair<String, Any?>)

将键值对列表转换为 Data 对象。

androidx.work.testing

依赖项

Groovy

dependencies {
    implementation "androidx.work:work-testing:2.9.0"
}

Kotlin

dependencies {
    implementation("androidx.work:work-testing:2.9.0")
}

顶级函数

TestListenableWorkerBuilder<W> TestListenableWorkerBuilder(context: Context, inputData: Data = Data.EMPTY, tags: List<String> = emptyList(), runAttemptCount: Int = 1, triggeredContentUris: List<Uri> = emptyList(), triggeredContentAuthorities: List<String> = emptyList())

构建 TestListenableWorkerBuilder 的实例。

TestWorkerBuilder<W> TestWorkerBuilder(context: Context, executor: Executor, inputData: Data = Data.EMPTY, tags: List<String> = emptyList(), runAttemptCount: Int = 1, triggeredContentUris: List<Uri> = emptyList(), triggeredContentAuthorities: List<String> = emptyList())

构建 TestWorkerBuilder 的实例。

com.google.android.play.core.ktx

依赖项

Groovy

dependencies {
    implementation "com.google.android.play:core-ktx:1.8.1"
}

Kotlin

dependencies {
    implementation("com.google.android.play:core-ktx:1.8.1")
}

扩展函数

对于 com.google.android.play.core.appupdate.AppUpdateManager
suspend AppUpdateInfo AppUpdateManager.requestAppUpdateInfo()

请求当前应用的更新可用性

suspend Unit AppUpdateManager.requestCompleteUpdate()

针对灵活更新流程,触发完成更新。

Flow<AppUpdateResult> AppUpdateManager.requestUpdateFlow()

入口点,用于监控更新的可用性和进度。

Boolean AppUpdateManager.startUpdateFlowForResult(appUpdateInfo: AppUpdateInfo, appUpdateType: Int, fragment: Fragment, requestCode: Int)

AppUpdateManager.startUpdateFlowForResult 的一个版本,可接受 AndroidX Fragment 以返回结果。

对于 com.google.android.play.core.splitinstall.SplitInstallManager
suspend Unit SplitInstallManager.requestCancelInstall(sessionId: Int)

挂起 SplitInstallManager.cancelInstall 的版本

suspend Unit SplitInstallManager.requestDeferredInstall(moduleNames: List<String>)

挂起 SplitInstallManager.deferredInstall 的版本

suspend Unit SplitInstallManager.requestDeferredLanguageInstall(languages: List<Locale>)

挂起 SplitInstallManager.deferredLanguageInstall 的版本

suspend Unit SplitInstallManager.requestDeferredLanguageUninstall(languages: List<Locale>)

挂起 SplitInstallManager.deferredLanguageUninstall 的版本

suspend Unit SplitInstallManager.requestDeferredUninstall(moduleNames: List<String>)

挂起 SplitInstallManager.deferredUninstall 的版本

suspend Int SplitInstallManager.requestInstall(modules: List<String> = listOf(), languages: List<String> = listOf())

启动所请求模块/语言的安装。

Flow<SplitInstallSessionState> SplitInstallManager.requestProgressFlow()

创建并返回缓冲的流程,这将会提供正在进行的拆分安装的所有进度事件。

suspend SplitInstallSessionState SplitInstallManager.requestSessionState(sessionId: Int)

挂起 SplitInstallManager.getSessionState 的版本

suspend List<SplitInstallSessionState> SplitInstallManager.requestSessionStates()

挂起 SplitInstallManager.getSessionStates 的版本

Boolean SplitInstallManager.startConfirmationDialogForResult(sessionState: SplitInstallSessionState, fragment: Fragment, requestCode: Int)

SplitInstallManager.startConfirmationDialogForResult 的一个版本,可接受 AndroidX Fragment 以返回结果的。

扩展属性

对于 com.google.android.play.core.appupdate.AppUpdateInfo
Int AppUpdateInfo.installStatus()

返回更新的进度状态。

Boolean AppUpdateInfo.isFlexibleUpdateAllowed()

如果允许进行灵活更新,则返回 true

Boolean AppUpdateInfo.isImmediateUpdateAllowed()

如果允许立即更新,则返回 true

对于 com.google.android.play.core.install.InstallState
Boolean InstallState.hasTerminalStatus()

这表明此为终端状态(不会再进行更新),并且应该进行相应处理(成功、取消或失败)。

Int InstallState.installErrorCode()

返回安装的错误代码,或 {@link InstallErrorCode#NO_ERROR}。

Int InstallState.installStatus()

返回安装的状态。

String! InstallState.packageName()

返回正在安装的应用的软件包名称。

对于 com.google.android.play.core.splitinstall.SplitInstallSessionState
Long SplitInstallSessionState.bytesDownloaded()

该更新所下载的字节数。

Int SplitInstallSessionState.errorCode()

该更新的错误代码。

Boolean SplitInstallSessionState.hasTerminalStatus()

表明该更新为终端状态,这意味着将不会再针对此会话进行更新。

List<String> SplitInstallSessionState.languages()

该更新包含的语言。

List<String> SplitInstallSessionState.moduleNames()

该更新包含的模块。

Int SplitInstallSessionState.sessionId()

该更新的会话 ID。

Int SplitInstallSessionState.status()

该更新的状态代码。

Long SplitInstallSessionState.totalBytesToDownload()

该更新要下载的总字节数。

顶级函数

SplitInstallStateUpdatedListener SplitInstallStateUpdatedListener(onRequiresConfirmation: (SplitInstallSessionState) -> Unit, onInstalled: (SplitInstallSessionState) -> Unit, onFailed: (SplitInstallSessionState) -> Unit = {}, onPending: (SplitInstallSessionState) -> Unit = {}, onDownloaded: (SplitInstallSessionState) -> Unit = {}, onDownloading: (SplitInstallSessionState) -> Unit = {}, onInstalling: (SplitInstallSessionState) -> Unit = {}, onCanceling: (SplitInstallSessionState) -> Unit = {}, onCanceled: (SplitInstallSessionState) -> Unit = {}, onNonTerminalStatus: (SplitInstallSessionState) -> Unit = {}, onTerminalStatus: (SplitInstallSessionState) -> Unit = {})

用于创建 SplitInstallStateUpdatedListener 的便捷函数。