Kotlin 常用工具类

工具类一般方式为扩展函数,这里列的内容不多,如果有建议或者好的项目,欢迎留言。

与Context有关工具类

import android.app.Activity
import android.app.Dialog
import android.content.ClipData
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Parcel
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.annotation.*
import androidx.core.content.ContextCompat
import androidx.core.os.ParcelCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager

/**
 * Desc:Context相关拓展函数、顶层函数
 * Author: hy
 * Date: 2019/7/15 16:16
 **/

inline fun <reified T : Activity> Fragment.startActivity(vararg params: Pair<String, Any?>) =
        activity?.let {
            Internals.internalStartActivity(it, T::class.java, params)
        }

inline fun <reified T : Activity> Context.startActivity(vararg params: Pair<String, Any?>) =
        Internals.internalStartActivity(this, T::class.java, params)

inline fun <reified T : Activity> Activity.startActivity(vararg params: Pair<String, Any?>) =
        Internals.internalStartActivity(this, T::class.java, params)

inline fun <reified T : Activity> Fragment.startActivityForRes(requestCode: Int, vararg params: Pair<String, Any?>) =
        activity?.let {
            startActivityForResult(Internals.createIntent(it, T::class.java, params), requestCode)
        }

inline fun <reified T : Activity> Activity.startActivityForRes(requestCode: Int, vararg params: Pair<String, Any?>) =
        startActivityForResult(Internals.createIntent(this, T::class.java, params), requestCode)

fun Context.getDrawableByAttr(@AttrRes attr: Int): Drawable? {
    val ta = obtainStyledAttributes(intArrayOf(attr))
    val drawable = ta.getDrawable(0)
    ta.recycle()
    return drawable
}

fun Context.getThemeByAttr(@AttrRes attr: Int): Int {
    val ta = obtainStyledAttributes(intArrayOf(attr))
    val theme = ta.getResourceId(0, 0)
    ta.recycle()
    return theme
}

/**
 * 复制到粘贴板
 */
/*fun Context.copyText(string: String) {
    val cm = getSystemService(Context.CLIPBOARD_SERVICE)
            as android.content.ClipboardManager
    val clip = ClipData.newPlainText("", string)
    cm.primaryClip = clip
}*/


fun Context.getString(@StringRes id: Int): String = getString(id)

fun Fragment.inflate(@LayoutRes id: Int, root: ViewGroup? = null, attachToRoot: Boolean = false): View = LayoutInflater.from(context).inflate(id, root, attachToRoot)

fun Activity.inflate(@LayoutRes id: Int, root: ViewGroup? = null, attachToRoot: Boolean = false): View = LayoutInflater.from(this).inflate(id, root, attachToRoot)

fun View.inflate(@LayoutRes id: Int, root: ViewGroup? = null, attachToRoot: Boolean = false): View = LayoutInflater.from(this.context).inflate(id, root, attachToRoot)

fun Fragment.horizontalLinearLayoutManager() = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)

fun Fragment.verticalLinearLayoutManager() = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

fun Fragment.gridLayoutManager(spanCount: Int) = GridLayoutManager(context, spanCount)

fun Activity.horizontalLinearLayoutManager() = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)

fun Activity.verticalLinearLayoutManager() = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

fun Activity.gridLayoutManager(spanCount: Int) = GridLayoutManager(this, spanCount)

fun Context.horizontalLinearLayoutManager() = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)

fun Context.verticalLinearLayoutManager() = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

fun Context.gridLayoutManager(spanCount: Int) = GridLayoutManager(this, spanCount)

fun View.findColor(@ColorRes corRes: Int): Int = ContextCompat.getColor(context, corRes)
fun Fragment.findColor(@ColorRes corRes: Int): Int = ContextCompat.getColor(context!!, corRes)
fun Context.findColor(@ColorRes id: Int): Int = ContextCompat.getColor(this, id)
fun Activity.findColor(@ColorRes id: Int): Int = ContextCompat.getColor(this, id)

/**
 * 返回contentView
 */
inline val Activity.contentView: View?
    get() = findOptional<ViewGroup>(android.R.id.content)?.getChildAt(0)

inline fun <reified T : View> View.find(@IdRes id: Int): T = findViewById(id)
inline fun <reified T : View> Activity.find(@IdRes id: Int): T = findViewById(id)
inline fun <reified T : View> Fragment.find(@IdRes id: Int): T = view?.findViewById(id) as T
inline fun <reified T : View> Dialog.find(@IdRes id: Int): T = findViewById(id)

inline fun <reified T : View> View.findOptional(@IdRes id: Int): T? = findViewById(id) as? T
inline fun <reified T : View> Activity.findOptional(@IdRes id: Int): T? = findViewById(id) as? T
inline fun <reified T : View> Fragment.findOptional(@IdRes id: Int): T? = view?.findViewById(id) as? T
inline fun <reified T : View> Dialog.findOptional(@IdRes id: Int): T? = findViewById(id) as? T

fun <T : Fragment> T.withArguments(params: Array<out Pair<String, Any?>>): T {
    arguments = Internals.bundleOf(params)
    return this
}


/**
 * 显示软键盘
 */
fun View.showSoftInput() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    postDelayed({
        requestFocus()
        imm.showSoftInput(this, InputMethodManager.SHOW_FORCED)
    }, 60)
}

/**
 * 隐藏软键盘
 */
fun View.hideSoftInput() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.windowToken, 0)
}

fun Activity.hideSoftInput() {
    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    if (currentFocus != null) {
        imm.hideSoftInputFromWindow(currentFocus!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }
}

/** Write a boolean to a Parcel. */
fun Parcel.writeBooleanUsingCompat(value: Boolean) = ParcelCompat.writeBoolean(this, value)

/** Read a boolean from a Parcel. */
fun Parcel.readBooleanUsingCompat() = ParcelCompat.readBoolean(this)

上面代码关联的Internals,实际跳转逻辑

import android.app.Activity
import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import java.io.Serializable

/**
 * Desc:
 * Author: hy
 * Date: 2019/7/15 16:54
 **/

object Internals {

    @JvmStatic
    fun <T> createIntent(ctx: Context, clazz: Class<out T>, params: Array<out Pair<String, Any?>>): Intent {
        val intent = Intent(ctx, clazz)
        if (params.isNotEmpty()) fillIntentArguments(intent, params)
        return intent
    }

    @JvmStatic
    fun internalStartActivity(
            ctx: Context,
            activity: Class<out Activity>,
            params: Array<out Pair<String, Any?>>
    ) {
        ctx.startActivity(createIntent(ctx, activity, params))
    }

    @JvmStatic
    fun internalStartActivityForResult(
            act: Activity,
            activity: Class<out Activity>,
            requestCode: Int,
            params: Array<out Pair<String, Any?>>
    ) {
        act.startActivityForResult(createIntent(act, activity, params), requestCode)
    }

    @JvmStatic
    fun internalStartService(
            ctx: Context,
            service: Class<out Service>,
            params: Array<out Pair<String, Any?>>
    ): ComponentName? = ctx.startService(createIntent(ctx, service, params))

    @JvmStatic
    fun internalStopService(
            ctx: Context,
            service: Class<out Service>,
            params: Array<out Pair<String, Any?>>
    ): Boolean = ctx.stopService(createIntent(ctx, service, params))

    @JvmStatic
    private fun fillIntentArguments(intent: Intent, params: Array<out Pair<String, Any?>>) {
        params.forEach {
            when (val value = it.second) {
                null -> intent.putExtra(it.first, null as Serializable?)
                is Int -> intent.putExtra(it.first, value)
                is Long -> intent.putExtra(it.first, value)
                is CharSequence -> intent.putExtra(it.first, value)
                is String -> intent.putExtra(it.first, value)
                is Float -> intent.putExtra(it.first, value)
                is Double -> intent.putExtra(it.first, value)
                is Char -> intent.putExtra(it.first, value)
                is Short -> intent.putExtra(it.first, value)
                is Boolean -> intent.putExtra(it.first, value)
                is Serializable -> intent.putExtra(it.first, value)
                is Bundle -> intent.putExtra(it.first, value)
                is Parcelable -> intent.putExtra(it.first, value)
                is Array<*> -> when {
                    value.isArrayOf<CharSequence>() -> intent.putExtra(it.first, value)
                    value.isArrayOf<String>() -> intent.putExtra(it.first, value)
                    value.isArrayOf<Parcelable>() -> intent.putExtra(it.first, value)
                    else -> throw Exception("Intent extra ${it.first} has wrong type ${value.javaClass.name}")
                }
                is IntArray -> intent.putExtra(it.first, value)
                is LongArray -> intent.putExtra(it.first, value)
                is FloatArray -> intent.putExtra(it.first, value)
                is DoubleArray -> intent.putExtra(it.first, value)
                is CharArray -> intent.putExtra(it.first, value)
                is ShortArray -> intent.putExtra(it.first, value)
                is BooleanArray -> intent.putExtra(it.first, value)
                else -> throw Exception("Intent extra ${it.first} has wrong type ${value.javaClass.name}")
            }
            return@forEach
        }
    }

    fun bundleOf(params: Array<out Pair<String, Any?>>): Bundle {
        val b = Bundle()
        for (p in params) {
            val (k, v) = p
            when (v) {
                null -> b.putSerializable(k, null)
                is Boolean -> b.putBoolean(k, v)
                is Byte -> b.putByte(k, v)
                is Char -> b.putChar(k, v)
                is Short -> b.putShort(k, v)
                is Int -> b.putInt(k, v)
                is Long -> b.putLong(k, v)
                is Float -> b.putFloat(k, v)
                is Double -> b.putDouble(k, v)
                is String -> b.putString(k, v)
                is CharSequence -> b.putCharSequence(k, v)
                is Parcelable -> b.putParcelable(k, v)
                is Serializable -> b.putSerializable(k, v)
                is BooleanArray -> b.putBooleanArray(k, v)
                is ByteArray -> b.putByteArray(k, v)
                is CharArray -> b.putCharArray(k, v)
                is DoubleArray -> b.putDoubleArray(k, v)
                is FloatArray -> b.putFloatArray(k, v)
                is IntArray -> b.putIntArray(k, v)
                is LongArray -> b.putLongArray(k, v)
                is Array<*> -> {
                    @Suppress("UNCHECKED_CAST")
                    when {
                        v.isArrayOf<Parcelable>() -> b.putParcelableArray(k, v as Array<out Parcelable>)
                        v.isArrayOf<CharSequence>() -> b.putCharSequenceArray(k, v as Array<out CharSequence>)
                        v.isArrayOf<String>() -> b.putStringArray(k, v as Array<out String>)
                        else -> throw Exception("Unsupported bundle component (${v.javaClass})")
                    }
                }
                is ShortArray -> b.putShortArray(k, v)
                is Bundle -> b.putBundle(k, v)
                else -> throw Exception("Unsupported bundle component (${v.javaClass})")
            }
        }
        return b
    }


}

和屏幕相关工具类

import android.content.Context
import android.content.res.Resources
import android.util.TypedValue

/**
 * Desc:
 * Author: hy
 * Date: 2019/4/8 14:54
 **/

private val displayMetrics = Resources.getSystem().displayMetrics

val Float.dp: Float
    @JvmName("dp2px")
    get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, displayMetrics)

val Int.dp: Int
    @JvmName("dp2px")
    get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), displayMetrics).toInt()

val Float.sp: Float
    @JvmName("sp2px")
    get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this, displayMetrics)

val Int.sp: Int
    @JvmName("sp2px")
    get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.toFloat(), displayMetrics).toInt()

val Number.px: Number
    get() = this

val Number.px2dp: Int
    @JvmName("px2dp")
    get() = (this.toFloat() / displayMetrics.density).toInt()

val Number.px2sp: Int
    @JvmName("px2sp")
    get() = (this.toFloat() / displayMetrics.scaledDensity).toInt()

val SCREEN_WIDTH: Int
    @JvmName("SCREEN_WIDTH")
    get() = displayMetrics.widthPixels

val SCREEN_HEIGHT: Int
    @JvmName("SCREEN_HEIGHT")
    get() = displayMetrics.heightPixels

val STATUS_BAR_HEIGHT: Int
    @JvmName("STATUS_BAR_HEIGHT")
    get() {
        val resourceId = Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android")
        return Resources.getSystem().getDimensionPixelSize(resourceId)
    }

val Context.ACTION_BAR_HEIGHT: Int
    @JvmName("ACTION_BAR_HEIGHT")
    get() {
        val tv = TypedValue()
        return if (theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)
        } else 0
    }
val NAVIGATION_BAR_HEIGHT: Int
    @JvmName("NAVIGATION_BAR_HEIGHT")
    get() {
        val resourceId = Resources.getSystem().getIdentifier("navigation_bar_height", "dimen", "android")
        return if (resourceId != 0) {
            Resources.getSystem().getDimensionPixelOffset(resourceId)
        } else {
            0
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Kotlin SharePreferences是一个工具类,用于简化在Android应用程序中使用SharePreferences存储和获取数据的过程。SharePreferences是Android系统提供的一种轻量级的存储方式,适用于存储一些简单的键值对数据。 使用Kotlin SharePreferences工具类可以方便地进行数据的存储和读取。首先,我们需要创建一个SharePreferences实例: ```kotlin val sharedPreferences = context.getSharedPreferences("MyApp", Context.MODE_PRIVATE) ``` 其中,"MyApp"是SharePreferences的名称,Context.MODE_PRIVATE表示访问权限为私有。 接下来,可以通过实例化SharePreferences工具类来进行数据的存储和读取操作。以下是存储数据的示例代码: ```kotlin val editor = sharedPreferences.edit() editor.putString("username", "John") editor.putInt("age", 25) editor.apply() ``` 上述代码中,我们首先调用edit()方法获取到一个Editor实例,然后使用putString()和putInt()等方法来存储数据,最后调用apply()方法将数据提交到SharePreferences中。 接下来是读取数据的示例代码: ```kotlin val username = sharedPreferences.getString("username", "") val age = sharedPreferences.getInt("age", 0) ``` 上述代码中,我们可以通过getString()和getInt()等方法来读取之前存储的数据,第一个参数是键名,第二个参数是默认值(在键名对应的数据不存在时使用)。 Kotlin SharePreferences工具类还提供了其他一些常用的方法,如remove()用于删除某个键值对数据,contains()用于判断是否包含某个键等。 总之,Kotlin SharePreferences工具类可以帮助我们简化SharePreferences的使用,提供了方便的数据存储和读取操作,是Android开发中常用的数据持久化解决方案之一。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值