Skip to content
Android常用工具类(基于Kotlin)
Kotlin Java
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app
core
gradle/wrapper
.gitignore
LICENSE
README-EN.md
README.md
_config.yml
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

Android常用工具库&Kotlin扩展Kotlin Version

简体中文 English

开始使用

  • 将以下依赖项添加到module下的build.gradle
dependencies {
    implementation 'com.oikawaii.library:core:1.1.2'
}
  • 使用前需要在Application中初始化app变量
import com.oikawaii.library.core.android.app

class App : Application() {

    override fun onCreate() {
        app = this
        super.onCreate()
    }

}

API List

android.ktx

Activity

Extension about Activity

  • 跳转到指定Activity

    fun Context.startActivity(cls: KClass<*>, body: Intent.() -> Unit = {})
    fun Fragment.startActivity(cls: KClass<*>, body: Intent.() -> Unit = {})
    fun Activity.startActivityForResult(cls: KClass<*>, requestCode: Int, options: Bundle? = null, body: Intent.() -> Unit = {})
    fun Fragment.startActivityForResult(cls: KClass<*>, requestCode: Int, options: Bundle? = null, body: Intent.() -> Unit = {})
  • 打开应用详情页

    fun Context.startAppDetails()
    fun Fragment.startAppDetails()
    fun Activity.startAppDetailsForResult(requestCode: Int, options: Bundle?)
    fun Fragment.startAppDetailsForResult(requestCode: Int, options: Bundle?)
  • 打开储存管理

    fun Context.startStorageSettings()
    fun Fragment.startStorageSettings()
    fun Activity.startStorageSettingsForResult(requestCode: Int, options: Bundle?)
    fun Fragment.startStorageSettingsForResult(requestCode: Int, options: Bundle?)
  • 打开DOCUMENT TREE

    fun Context.startDocumentTree()
    fun Fragment.startDocumentTree()
    fun Activity.startDocumentTreeForResult(requestCode: Int, options: Bundle?)
    fun Fragment.startDocumentTreeForResult(requestCode: Int, options: Bundle?)

Application

Application Extension

  • 获取Application ID/Package Name

    val Application.id: String
  • 获取Application Uri

    val Application.uri: Uri
  • 判断设备是否处于竖屏状态

    val Application.isPortrait: Boolean
  • 判断设备是否处于横屏状态

    val Application.isLandscape: Boolean
  • 获取默认SharedPreferences

    val Application.sharedPrefs: SharedPreferences
  • 通过Application获取SharedPreferences

    fun Application.sharedPrefs(name: String): SharedPreferences

Base64

Base64相关扩展

  • 将ByteArray进行Base64编码(Default)

    val ByteArray.toBase64: ByteArray
    val String.toBase64: String
  • 将ByteArray进行Base64解码(Default)

    val ByteArray.fromBase64: ByteArray
    val String.fromBase64: String
  • 将ByteArray进行Base64编码

    fun ByteArray.toBase64(flag: Int): ByteArray
    fun String.toBase64(flag: Int): String
  • 将ByteArray进行Base64解码

    fun ByteArray.fromBase64(flag: Int): ByteArray
    fun String.fromBase64(flag: Int): String

Drawable

Drawable Extension

  • Creates a new Drawable instance from its constant state.

    val Drawable.new: Drawable
  • Make this drawable mutable.

    val Drawable.mutate: Drawable
  • Specifies tint color for this drawable.

    fun Drawable.tint(color: Int, mode: Mode): Drawable
  • Specifies tint color for this drawable as a color state list.

    fun Drawable.tintList(colors: ColorStateList, mode: Mode): Drawable

Fragment

Fragment Extension

  • FragmentTransaction中执行action并在结束时根据allowStateLossnow的值自动commit

    fun FragmentManager.commit(allowStateLoss: Boolean, now: Boolean, action: FragmentTransaction.() -> Unit)
    fun FragmentActivity.commit(allowStateLoss: Boolean, now: Boolean, action: FragmentTransaction.() -> Unit)
    fun Fragment.commit(allowStateLoss: Boolean, now: Boolean, action: FragmentTransaction.() -> Unit)
  • 通过tag移除被owner管理的Fragment

    fun FragmentTransaction.remove(owner: FragmentManager, tag: String)
    fun FragmentTransaction.remove(owner: FragmentActivity, tag: String)
    fun FragmentTransaction.remove(owner: Fragment, tag: String)

Handler

Handler Extension

  • 发送Empty Handler Message

    fun Handler.msg(what: Int)
  • 发送Handler Message

    fun Handler.msg(what: Int, obj: Any?, arg1: Int?, arg2: Int?)
  • 在短过渡动画执行完毕后mills耗秒执行action

    fun Handler.afterShortAnimTime(mills: Int, token: Any?, crossinline action: () -> Unit)
  • 在中过渡动画执行完毕后mills耗秒执行action

    fun Handler.afterShortAnimTime(mills: Int, token: Any?, crossinline action: () -> Unit)
  • 在长过渡动画执行完毕后mills耗秒执行action

    fun Handler.afterShortAnimTime(mills: Int, token: Any?, crossinline action: () -> Unit)

Lifecycle

Lifecycle Extension

  • 返回或新建一个ViewModel

    fun Fragment.findViewModel(cls: KClass<T>): T
    fun FragmentActivity.findViewModel(cls: KClass<T>): T
  • Observer对象注册到LiveData对象

    fun LiveData<T>.observe(owner: LifecycleOwner, crossinline observer: (it: T) -> Unit)

String

String Extension

  • 返回一个带有删除线的字符串
    val String.strikethrough: SpannableString

System Service Manager

Extension about System Service

  • 获取Service Manager
    val Application.accessibilityManager: AccessibilityManager
    val Application.accountManager: AccountManager
    val Application.activityManager: ActivityManager
    val Application.alarmManager: AlarmManager
    val Application.audioManager: AudioManager
    val Application.clipboardManager: ClipboardManager
    val Application.connectivityManager: ConnectivityManager
    val Application.devicePolicyManager: DevicePolicyManager
    val Application.downloadManager: DownloadManager
    val Application.dropBoxManager: DropBoxManager
    val Application.inputManager: InputManager
    val Application.inputMethodManager: InputMethodManager
    val Application.keyguardManager: KeyguardManager
    val Application.locationManager: LocationManager
    val Application.nfcManager: NfcManager
    val Application.notificationManager: NotificationManager
    val Application.powerManager: PowerManager
    val Application.searchManager: SearchManager
    val Application.sensorManager: SensorManager
    val Application.storageManager: StorageManager
    val Application.telephonyManager: TelephonyManager
    val Application.uiModeManager: UiModeManager
    val Application.usbManager: UsbManager
    val Application.wallpaperManager: WallpaperManager
    val Application.wifiManager: WifiManager
    val Application.wifiP2pManager: WifiP2pManager
    val Application.windowManager: WindowManager
    val Application.userManager: UserManager
    val Application.displayManager: DisplayManager
    val Application.bluetoothManager: BluetoothManager
    val Application.captioningManager: CaptioningManager
    val Application.appOpsManager: AppOpsManager
    val Application.printManager: PrintManager
    val Application.consumerManager: ConsumerIrManager
    val Application.telecomManager: TelecomManager
    val Application.cameraManager: CameraManager
    val Application.batteryManager: BatteryManager
    val Application.appWidgetManager: AppWidgetManager
    val Application.restrictionsManager: RestrictionsManager
    val Application.mediaSessionManager: MediaSessionManager
    val Application.mediaProjectionManager: MediaProjectionManager
    val Application.usageStatsManager: UsageStatsManager
    val Application.midiManager: MidiManager
    val Application.networkStatusManager: NetworkStatsManager
    val Application.carrierConfigManager: CarrierConfigManager
    val Application.systemHealthManager: SystemHealthManager
    val Application.hardwarePropertiesManager: HardwarePropertiesManager
    val Application.shortcutManager: ShortcutManager
    val Application.storageStatsManager: StorageStatsManager
    val Application.companionDeviceManager: CompanionDeviceManager
    val Application.textClassificationManager: TextClassificationManager
    val Application.euiccManager: EuiccManager
    val Application.ipSecManager: IpSecManager

android.util

ColorUtil

与颜色有关的工具类

  • 获取与设置颜色值

    var color: Int
  • 获取colorred通道值(red ∈ [0,255])

    val red: Int
  • 获取colorgreen通道值(green ∈ [0,255])

    val green: Int
  • 获取colorblue通道值(blue ∈ [0,255])

    val blue: Int
  • 获取与设置coloralpha通道值(alpha ∈ [0,255])

    var alpha: Int
  • 获取color的灰阶(grayscale ∈ [0,255])

    val grayscale: Int
  • 获取color的十六进制码(hex ∈ [#00000000,#FFFFFFFF])

    val hex: Int
  • 获取color透过遮罩mask所应显示的颜色

    fun composite(mask: Int): Int

DimenUtil

Dimen工具类

  • 获取虚拟键的高度,单位px。Android内部提供的方法,但某些魔改ROM在隐藏虚拟键时会失效(e.g. One UI)。

    val navigation: Int
  • 获取虚拟键的高度,单位px。通过屏幕尺寸减去显示尺寸获得。

    fun navigation(): Int
  • 获取状态栏的高度(px)

    val status: Int
  • 获取内容高度(不包含虚拟键与状态栏的尺寸的屏幕高度)

    val height: Int
  • 根据条件获取屏幕高度,statustrue时计算状态栏尺寸,navigationtrue时计算虚拟键尺寸。

    fun height(status: Boolean, navigation: Boolean): Int
  • 获取内容宽度(不包含虚拟键的尺寸的屏幕宽度)

    val width: Int
  • 根据条件获取屏幕宽度,navigationtrue时计算虚拟键尺寸。

    fun width(navigation: Boolean): Int
  • 将单位转换为px

    fun px(value: Float, unit: Int): Int
  • px转dp

    fun dp(px: Int): Float
  • px转pt

    fun pt(px: Int): Float
  • px转mm

    fun mm(px: Int): Float
  • px转inch

    fun inch(px: Int): Float

DrawableUtil

Drawable工具类

  • 创建LayerDrawable
    fun layer(vararg drawables: Drawable): LayerDrawable

LogUtil

Android Logcat工具类

  • 发送一个VERBOSE日志消息

    fun v(tag: Any?, vararg message: Any?): LogUtil
    fun v(tag: Any?, tr: Throwable, vararg message: Any?): LogUtil
  • 发送一个INFO日志消息

    fun i(tag: Any?, vararg message: Any?): LogUtil
    fun i(tag: Any?, tr: Throwable, vararg message: Any?): LogUtil
  • 发送一个DEBUG日志消息

    fun d(tag: Any?, vararg message: Any?): LogUtil
    fun d(tag: Any?, tr: Throwable, vararg message: Any?): LogUtil
  • 发送一个WARN日志消息

    fun w(tag: Any?, vararg message: Any?): LogUtil
    fun w(tag: Any?, tr: Throwable, vararg message: Any?): LogUtil
  • 发送一个ERROR日志消息

    fun e(tag: Any?, vararg message: Any?): LogUtil
    fun e(tag: Any?, tr: Throwable, vararg message: Any?): LogUtil

ResUtil

获取Resource ID代表的Int值

  • 获取Resource ID代表的Int值

    fun getInt(id: Int): Int
  • 获取Resource ID代表的String值

    fun getString(id: Int): String
  • 获取Resource ID代表的String值并格式化

    fun getString(id: Int, vararg args: Any?): String
  • 获取Resource ID代表的Quantity String

    fun getString(id: Int, quantity: Int): String
  • 获取Resource ID代表的Quantity String并格式化

    fun getString(id: Int, quantity: Int, vararg args: Any?): String
  • 获取Resource ID代表的Boolean值

    fun getBool(id: Int): Boolean
  • 获取Resource ID代表的Dimension值

    fun getDimension(id: Int): Float
  • 获取Resource ID代表的Pixel值

    fun getPixel(id: Int): Int
  • 获取Resource ID代表的Color值

    fun getColor(id: Int): Int
  • 获取Resource ID代表的Drawable值

    fun getDrawable(id: Int): Drawable?

StateListUtil

StateList utility methods

  • 新建DrawableState

    fun colorStateOf(vararg pairs: Pair<Int, Int>): ColorStateList
  • 新建DrawableState

    fun drawableStateOf(vararg pairs: Pair<Int, Drawable?>): StateListDrawable

SystemUiUtil

与虚拟键&状态栏有关的工具类

  • 深浅主题判断参数,默认为178(grayscale > 178时为浅色背景,将会自动切换为深色 虚拟键按钮/状态栏图标)。

    var grayscale: Int
  • 设置虚拟键和状态栏颜色&主题

    fun setSystemBar(color: Int): SystemUiUtil
  • 设置虚拟键和状态栏主题

    fun setSystemBarStyle(color: Int): SystemUiUtil
  • 设置状态栏颜色&主题

    fun setStatusBar(color: Int): SystemUiUtil
  • 设置状态栏主题

    fun setStatusBarStyle(color: Int): SystemUiUtil
  • 设置虚拟键颜色&主题

    fun setNavigationBar(color: Int): SystemUiUtil
  • 设置虚拟键主题

    fun setNavigationBarStyle(color: Int): SystemUiUtil
  • 隐藏系统栏,statustrue时隐藏状态栏,navigationtrue时隐藏虚拟键,stickfalse时隐藏后再次呼出系统栏后系统栏不会再次隐藏,stabletrue时添加View.SYSTEM_UI_FLAG_LAYOUT_STABLE

    fun hideSystemBar(status: Boolean, navigation: Boolean, stick: Boolean, stable: Boolean = true): SystemUiUtil
  • 透明化系统栏,允许布局侵入到虚拟键&状态栏

    fun translucentSystemBar(switch: Boolean): SystemUiUtil
  • 透明化状态栏,允许布局侵入到状态栏

    fun translucentStatusBar(switch: Boolean): SystemUiUtil

ToastUtil

Toast工具类

  • 显示短Toast,并取消上一个

    fun makeShort(res: Int)
    fun makeShort(str: String)
  • 显示长Toast,并取消上一个&主题

    fun makeLong(res: Int)
    fun makeLong(str: String)
  • 显示Toast,并取消上一个

    fun show(context: Context, res: Int, duration: Int)
    fun show(context: Context, str: String, duration: Int)

kotlin.ktx

Array

Array Extension

  • 将Array<T>转换为ArrayList<T>

    fun Array<T>.toArrayList(): ArrayList<T>
  • 将Array<T>转换为Array<R>

    fun Array<T>.toArray(transform: (T) -> R): Array<R>
    fun Array<T>.toByteArray(transform: (T) -> Byte): ByteArray
    fun Array<T>.toShortArray(transform: (T) -> Short): ShortArray
    fun Array<T>.toIntArray(transform: (T) -> Int): IntArray
    fun Array<T>.toLongArray(transform: (T) -> Long): LongArray
    fun Array<T>.toFloatArray(transform: (T) -> Float): FloatArray
    fun Array<T>.toDoubleArray(transform: (T) -> Double): DoubleArray
    fun Array<T>.toBooleanArray(transform: (T) -> Boolean): BooleanArray
    fun Array<T>.toCharArray(transform: (T) -> Char): CharArray
  • 将Array<T>转换为ArrayList<R>

    fun Array<T>.toArrayList(transform: (T) -> R): ArrayList<R>
  • 将Array<T>过滤并转换为ArrayList<R>

    fun Array<T>.filterToArrayList(predicate: (T) -> R): ArrayList<T>
    fun ByteArray.filterToArrayList(predicate: (Byte) -> Boolean): ArrayList<Byte>
    fun ShortArray.filterToArrayList(predicate: (Short) -> Boolean): ArrayList<Short>
    fun IntArray.filterToArrayList(predicate: (Int) -> Boolean): ArrayList<Int>
    fun LongArray.filterToArrayList(predicate: (Long) -> Boolean): ArrayList<Long>
    fun FloatArray.filterToArrayList(predicate: (Float) -> Boolean): ArrayList<Float>
    fun DoubleArray.filterToArrayList(predicate: (Double) -> Boolean): ArrayList<Double>
    fun BooleanArray.filterToArrayList(predicate: (Boolean) -> Boolean): ArrayList<Boolean>
    fun CharArray.filterToArrayList(predicate: (Char) -> Boolean): ArrayList<Char>
  • 以Long型的方式对Array中的元素求和

    fun Array<T>.sumByLong(selector: (T) -> Long): Long
    fun ByteArray.sumByLong(selector: (Byte) -> Long): Long
    fun ShortArray.sumByLong(selector: (Short) -> Long): Long
    fun IntArray.sumByLong(selector: (Int) -> Long): Long
    fun LongArray.sumByLong(selector: (Long) -> Long): Long
    fun FloatArray.sumByLong(selector: (Float) -> Long): Long
    fun DoubleArray.sumByLong(selector: (Double) -> Long): Long
    fun BooleanArray.sumByLong(selector: (Boolean) -> Long): Long
    fun CharArray.sumByLong(selector: (Char) -> Long): Long
  • 以Float型的方式对Array中的元素求和

    fun Array<T>.sumByFloat(selector: (T) -> Float): Float
    fun ByteArray.sumByFloat(selector: (Byte) -> Float): Float
    fun ShortArray.sumByFloat(selector: (Short) -> Float): Float
    fun IntArray.sumByFloat(selector: (Int) -> Float): Float
    fun LongArray.sumByFloat(selector: (Long) -> Float): Float
    fun FloatArray.sumByFloat(selector: (Float) -> Float): Float
    fun DoubleArray.sumByFloat(selector: (Double) -> Float): Float
    fun BooleanArray.sumByFloat(selector: (Boolean) -> Float): Float
    fun CharArray.sumByFloat(selector: (Char) -> Float): Float
  • 将ByteArray转换为UTF-8格式的String(ByteArray.toString(Charsets.UTF_8))

    val ByteArray.toUTF8: String
  • 将ByteArray进行Base64编码

    val ByteArray.toBase64: ByteArray
  • 将ByteArray进行Base64解码

    val ByteArray.fromBase64: ByteArray
  • 将ByteArray进行Base64编码(Url and Filename Safe)

    val ByteArray.toUrlBase64: ByteArray
  • 将ByteArray进行Base64解码(Url and Filename Safe)

    val ByteArray.fromUrlBase64: ByteArray
  • 将ByteArray进行Base64编码(Mime type)

    val ByteArray.toMimeBase64: ByteArray
  • 将ByteArray进行Base64解码(Mime type)

    val ByteArray.fromMimeBase64: ByteArray
  • 将ByteArray转化为正的BigInteger

    val ByteArray.toPositiveBigInteger: BigInteger
    //ByteArray(1) { 0x7F }.toPositiveBigInteger.toDecimal -> 127
  • 将ByteArray转化为负的BigInteger

    val ByteArray.toNegativeBigInteger: BigInteger
    //ByteArray(1) { 0x7F }.toNegativeBigInteger.toDecimal -> -127

BigInteger

BigInteger Extension

  • 将BigInteger类型转换为二进制值(BigInteger.toString(2))

    val BigInteger.toBinary: String
    //BigInteger(1, ByteArray(1) { 0xF }).toBinary -> 1111
  • 将BigInteger类型转换为八进制值(BigInteger.toString(8))

    val BigInteger.toOctal: String
    //BigInteger(1, ByteArray(1) { 0xF }).toOctal -> 17
  • 将BigInteger类型转换为十进制值(BigInteger.toString(10))

    val BigInteger.toDecimal: String
    //BigInteger(1, ByteArray(1) { 0xF }).toDecimal -> 15
  • 将BigInteger类型转换为十六进制值(BigInteger.toString(16))

    val BigInteger.toHexadecimal: String
    //BigInteger(1, ByteArray(1) { 0xF }).toHexadecimal -> f

Boolean

Boolean Extension

  • 将Boolean型转换为Int型
    val Boolean.toInt: Int
    //true.toInt -> 1
    //false.toInt -> 0

Calendar

Calendar Extension

  • 协调世界时(UTC时间)

    val Calendar.utc: String
  • 格式化日期(默认格式:"yyyy-MM-dd HH:mm:ss")

    fun Calendar.date(pattern: String, locale: Locale): String
  • val Calendar.year: Int
  • val Calendar.month: Int
  • val Calendar.day: Int
  • val Calendar.hour: Int
  • val Calendar.minute: Int
  • val Calendar.second: Int
  • 毫秒

    val Calendar.millisecond: Int
  • 星期(Int)

    val Calendar.week: Int
  • 星期(String)

    fun Calendar.week(locale: Locale): String
  • 将日历定位到一周的开始

    fun Calendar.firstOfWeek(firstDayOfWeek: Int): Calendar
  • 将日历定位到一周的结束

    fun Calendar.lastOfWeek(firstDayOfWeek: Int): Calendar
  • 获取月周次

    val Calendar.weekOfMonth: Int
  • 获取年周次

    val Calendar.weekOfYear: Int

Class

Class Extension

  • 获取所有 Fields 的名称

    val Class<out T>.fieldsName: Array<String>
  • 获取所有 Declared Fields 的名称

    val Class<out T>.declaredFieldsName: Array<String>
  • 获取所有 Methods 的方法名

    val Class<out T>.methodsName: Array<String>
  • 获取所有 Declared Methods 的方法名

    val Class<out T>.declaredMethodsName: Array<String>

Int

Int Extension

  • 将Int类型转换为Boolean

    val Int.toBoolean: Boolean
    //1.toBoolean -> true
    //0.toBoolean -> false
    //(-1).toBoolean -> true
  • 将Int类型转换为二进制值(Int.toString(2))

    val Int.toBinary: String
    //0xF.toBinary -> 1111
  • 将Int类型转换为八进制值(Int.toString(8))

    val Int.toOctal: String
    //0xF.toOctal -> 17
  • 将Int类型转换为十进制值(Int.toString(10))

    val Int.toDecimal: String
    //0xF.toDecimal -> 15
  • 将Int类型转换为十六进制值(Int.toString(16))

    val Int.toHexadecimal: String
    //0xF.toHexadecimal -> f

Iterable

Iterable Extension

  • 将Iterable<T>转换为ArrayList<T>

    fun Iterable<T>.toArrayList(): ArrayList<T>
  • 将Iterable<T>转换为Array<R>

    fun Iterable<T>.toArray(transform: (T) -> R): Array<R>
    fun Iterable<T>.toByteArray(transform: (T) -> Byte): ByteArray
    fun Iterable<T>.toShortArray(transform: (T) -> Short): ShortArray
    fun Iterable<T>.toIntArray(transform: (T) -> Int): IntArray
    fun Iterable<T>.toLongArray(transform: (T) -> Long): LongArray
    fun Iterable<T>.toFloatArray(transform: (T) -> Float): FloatArray
    fun Iterable<T>.toDoubleArray(transform: (T) -> Double): DoubleArray
    fun Iterable<T>.toBooleanArray(transform: (T) -> Boolean): BooleanArray
    fun Iterable<T>.toCharArray(transform: (T) -> Char): CharArray
  • 将Iterable<T>转换为ArrayList<R>

    fun Iterable<T>.toArrayList(transform: (T) -> R): ArrayList<R>
  • 将Iterable<T>过滤并转换为ArrayList<R>

    fun Iterable<T>.filterToArrayList(predicate: (T) -> R): ArrayList<T>
    //setOf(1, 2, 3, 4, 5).filterToArrayList { it % 2 == 0 } -> [2, 4]
  • 以Long型的方式对Iterable中的元素求和

    fun Iterable<T>.sumByLong(selector: (T) -> Long): Long
    //hashSetOf(1, 2, 3, 4, 5).sumByLong { it * 1L } -> 15
  • 以Float型的方式对Iterable中的元素求和

    fun Iterable<T>.sumByFloat(selector: (T) -> Float): Float
    //arrayListOf(1, 2, 3, 4, 5).sumByFloat { it * 1f } -> 15.0

Sequence

Sequence Extension

  • 将Sequence<T>转换为Array<T>

    fun Sequence<T>.toArray(): Array<T>
  • 将Sequence<T>转换为ArrayList<T>

    fun Sequence<T>.toArrayList(): ArrayList<T>
  • 将Sequence<T>转换为Array<R>

    fun Sequence<T>.toArray(transform: (T) -> R): Array<R>
    fun Sequence<T>.toByteArray(transform: (T) -> Byte): ByteArray
    fun Sequence<T>.toShortArray(transform: (T) -> Short): ShortArray
    fun Sequence<T>.toIntArray(transform: (T) -> Int): IntArray
    fun Sequence<T>.toLongArray(transform: (T) -> Long): LongArray
    fun Sequence<T>.toFloatArray(transform: (T) -> Float): FloatArray
    fun Sequence<T>.toDoubleArray(transform: (T) -> Double): DoubleArray
    fun Sequence<T>.toBooleanArray(transform: (T) -> Boolean): BooleanArray
    fun Sequence<T>.toCharArray(transform: (T) -> Char): CharArray
  • 将Sequence<T>转换为ArrayList<R>

    fun Sequence<T>.toArrayList(transform: (T) -> R): ArrayList<R>
  • 将Sequence<T>过滤并转换为ArrayList<R>

    fun Sequence<T>.filterToArrayList(predicate: (T) -> R): ArrayList<T>
    //sequenceOf(1, 2, 3, 4, 5).filterToArrayList { it % 2 == 0 } -> [2, 4]
  • 以Long型的方式对Sequence中的元素求和

    fun Sequence<T>.sumByLong(selector: (T) -> Long): Long
    //sequenceOf(1, 2, 3, 4, 5).sumByLong { it * 1L } -> 15
  • 以Float型的方式对Sequence中的元素求和

    fun Sequence<T>.sumByFloat(selector: (T) -> Float): Float
    //sequenceOf(1, 2, 3, 4, 5).sumByFloat { it * 1f } -> 15.0

String.kt

String Extension

  • 将String转化为UTF-8格式的ByteArray(String.toByteArray(Charsets.UTF_8))

    val String.toByteArray: ByteArray
  • 将字符串大写

    val String.toUppercase: String
    //"String".toUppercase -> STRING
  • 将字符串小写

    val String.toLowercase: String
    //"String".toLowercase -> string
  • 将String进行Base64编码

    val String.toBase64: String
  • 将String进行Base64解码

    val String.fromBase64: String
  • 将String进行Base64编码(Url and Filename Safe)

    val String.toUrlBase64: String
  • 将String进行Base64解码(Url and Filename Safe)

    val String.fromUrlBase64: String
  • 将String进行Base64编码(Mime type)

    val String.toMimeBase64: String
  • 将String进行Base64解码(Mime type)

    val String.fromMimeBase64: String

kotlin.util

ByteUtil

依照 IEEE 1541-2002 标准将 Byte 转换为 (X)B/(X)iB 的方法类。

  • 将字节转化为KB(1KB = 1000B)。

    fun toKB(): Double
    //ByteUtil(1000).toKB() -> 1.0
  • 将字节转化为MB(1KB = 1000B)。

    fun toMB(): Double
    //ByteUtil(1000000).toMB() -> 1.0
  • 将字节转化为GB(1KB = 1000B)。

    fun toGB(): Double
    //ByteUtil(1000000000).toGB() -> 1.0
  • 将字节转化为TB(1KB = 1000B)。

    fun toTB(): Double
    //ByteUtil(1000000000000).toTB() -> 1.0
  • 将字节转化为PB(1KB = 1000B)。

    fun toPB(): Double
    //ByteUtil(1000000000000000).toPB() -> 1.0
  • 将字节转化为EB(1KB = 1000B)。

    fun toEB(): Double
    //ByteUtil(1000000000000000000).toEB() -> 1.0
  • 将字节转化为KiB(1KiB = 1024B)。

    fun toKiB(): Double
    //ByteUtil(1024).toKiB() -> 1.0
  • 将字节转化为MiB(1KiB = 1024B)。

    fun toMiB(): Double
    //ByteUtil(1048576).toMiB() -> 1.0
  • 将字节转化为GiB(1KiB = 1024B)。

    fun toGiB(): Double
    //ByteUtil(1073741824).toGiB() -> 1.0
  • 将字节转化为TiB(1KiB = 1024B)。

    fun toTiB(): Double
    //ByteUtil(1099511627776).toTiB() -> 1.0
  • 将字节转化为PiB(1KiB = 1024B)。

    fun toPiB(): Double
    //ByteUtil(1125899906842624).toPiB() -> 1.0
  • 将字节转化为EiB(1KiB = 1024B)。

    fun toEiB(): Double
    //ByteUtil(1152921504606846976).toEiB() -> 1.0
  • 将字节自动转换为合适的二进制单位。

    fun autoBinary(): String
    //ByteUtil(1125899906842624).autoBinary() -> 1.00PiB
    //ByteUtil(1152921504606846976).autoBinary() -> 1.00EiB
  • 将字节自动转换为合适的十进制单位。

    fun autoDecimal(): String
    //ByteUtil(1000000000000000).autoDecimal() -> 1.00PB
    //ByteUtil(1000000000000000000).autoDecimal() -> 1.00EB
  • 将字节自动转换为合适的十进制或二进制单位。

    fun auto(decimal: Boolean): String
    //ByteUtil(1000000000000000000).auto(true) -> 1.00EB
    //ByteUtil(1152921504606846976).auto(false) -> 1.00EiB
You can’t perform that action at this time.