初始代码提交
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
package com.sw.inbound.utils.ext
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.content.res.Resources
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.GradientDrawable
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.TypedValue
|
||||
import android.view.View
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import android.widget.EditText
|
||||
import android.widget.Toast
|
||||
import androidx.core.app.ActivityOptionsCompat
|
||||
import androidx.core.graphics.toColorInt
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.sw.inbound.utils.CustomToastUtils
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import java.math.BigDecimal
|
||||
import java.math.RoundingMode
|
||||
|
||||
fun Context.toast(
|
||||
message: String?,
|
||||
duration: Int = Toast.LENGTH_SHORT,
|
||||
) {
|
||||
if (message.isNullOrBlank()) {
|
||||
return
|
||||
}
|
||||
Handler(Looper.getMainLooper()).post {
|
||||
//Toast.makeText(this, message, duration).show()
|
||||
CustomToastUtils.showToast(message, duration)
|
||||
}
|
||||
}
|
||||
|
||||
fun Fragment.toast(
|
||||
message: String?,
|
||||
duration: Int = 2000
|
||||
) {
|
||||
activity?.toast(message, duration)
|
||||
}
|
||||
|
||||
|
||||
fun View.visible() {
|
||||
visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
fun View.invisible() {
|
||||
visibility = View.INVISIBLE
|
||||
}
|
||||
|
||||
fun View.gone() {
|
||||
visibility = View.GONE
|
||||
}
|
||||
|
||||
inline fun <reified T : Activity> Context.startActivity(
|
||||
bundle: Bundle? = null,
|
||||
options: ActivityOptionsCompat? = null
|
||||
) {
|
||||
Intent(this, T::class.java).apply {
|
||||
bundle?.let { putExtras(it) }
|
||||
if (options != null && this@startActivity is Activity) {
|
||||
startActivity(this, options.toBundle())
|
||||
} else {
|
||||
startActivity(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T> Context.startActivity(
|
||||
block: Intent.() -> Unit = {}
|
||||
) {
|
||||
Intent(this, T::class.java).apply {
|
||||
block()
|
||||
startActivity(this)
|
||||
}
|
||||
}
|
||||
|
||||
//inline fun <reified T : Activity> Context.startActivity(action:(bundle: Bundle)-> Unit) {
|
||||
// Intent(this, T::class.java).apply {
|
||||
// action(Bundle())
|
||||
// startActivity(this)
|
||||
// }
|
||||
//}
|
||||
|
||||
inline fun <reified T> String.toType(gson: Gson? = null, typeToken: TypeToken<T>): T {
|
||||
return (gson ?: Gson()).fromJson(this, typeToken.type)
|
||||
}
|
||||
|
||||
inline fun <reified T> String.toObject(gson: Gson? = null): T {
|
||||
return (gson ?: Gson()).fromJson(this, T::class.java)
|
||||
}
|
||||
|
||||
fun Any?.toJsonString(gson: Gson? = null): String {
|
||||
return (gson ?: Gson()).toJson(this) ?: ""
|
||||
}
|
||||
|
||||
@SuppressLint("ApplySharedPref")
|
||||
inline fun SharedPreferences.edit(
|
||||
commit: Boolean = false,
|
||||
action: SharedPreferences.Editor.() -> Unit
|
||||
) {
|
||||
val editor = edit()
|
||||
action(editor)
|
||||
if (commit) editor.commit() else editor.apply()
|
||||
}
|
||||
|
||||
fun SharedPreferences.put(vararg pairs: Pair<String, Any>) {
|
||||
edit {
|
||||
pairs.forEach { (key, value) ->
|
||||
when (value) {
|
||||
is Int -> putInt(key, value)
|
||||
is String -> putString(key, value)
|
||||
is Boolean -> putBoolean(key, value)
|
||||
is Float -> putFloat(key, value)
|
||||
is Long -> putLong(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val Float.dp: Float
|
||||
get() = TypedValue.applyDimension(
|
||||
TypedValue.COMPLEX_UNIT_DIP,
|
||||
this,
|
||||
Resources.getSystem().displayMetrics
|
||||
)
|
||||
|
||||
val Int.dp: Int
|
||||
get() = this.toFloat().dp.toInt()
|
||||
|
||||
fun EditText.addOnActionSearchListener(searchCallback: () -> Unit) {
|
||||
setOnEditorActionListener { v, actionId, event ->
|
||||
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
||||
// 处理搜索逻辑
|
||||
searchCallback()
|
||||
return@setOnEditorActionListener true // 阻止事件继续传递
|
||||
}
|
||||
return@setOnEditorActionListener false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun View.clickWithDebounce(delay: Long = 300, action: () -> Unit) {
|
||||
var job: Job? = null
|
||||
setOnClickListener {
|
||||
job?.cancel()
|
||||
job = CoroutineScope(Dispatchers.Main).launch {
|
||||
delay(delay)
|
||||
action()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Double.roundedDecimalPlace(num: Int): Double {
|
||||
return BigDecimal(this).setScale(num, RoundingMode.HALF_UP).toDouble()
|
||||
}
|
||||
|
||||
fun Double.roundedOneDecimalPlace(): Double {
|
||||
return this.roundedDecimalPlace(1)
|
||||
}
|
||||
|
||||
fun View.setShapeDrawable(
|
||||
solidColor: String = "#FFFFFF",
|
||||
strokeWidth: Int = 0,
|
||||
strokeColor: String = "#FFFFFF",
|
||||
radius: Int = 0
|
||||
) {
|
||||
background = GradientDrawable().apply {
|
||||
setColor(solidColor.toColorInt())
|
||||
setStroke(strokeWidth, strokeColor.toColorInt())
|
||||
cornerRadius = radius.toFloat()
|
||||
}
|
||||
}
|
||||
|
||||
fun View.setShapeDrawable2(
|
||||
solidColor: String = "#FFFFFF",
|
||||
strokeWidth: Int = 0,
|
||||
strokeColor: String = "#FFFFFF",
|
||||
topLeftRadius: Int = 0,
|
||||
topRightRadius: Int = 0,
|
||||
bottomRightRadius: Int = 0,
|
||||
bottomLeftRadius: Int = 0
|
||||
) {
|
||||
background = GradientDrawable().apply {
|
||||
setColor(solidColor.toColorInt())
|
||||
setStroke(strokeWidth, strokeColor.toColorInt())
|
||||
|
||||
setCornerRadii(
|
||||
floatArrayOf(
|
||||
topLeftRadius.toFloat(), topLeftRadius.toFloat(),
|
||||
topRightRadius.toFloat(), topRightRadius.toFloat(),
|
||||
bottomRightRadius.toFloat(), bottomRightRadius.toFloat(),
|
||||
bottomLeftRadius.toFloat(), bottomLeftRadius.toFloat()
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun String?.ifNullOrBlank(defaultValue: String): String {
|
||||
return if (this.isNullOrBlank()) defaultValue else this
|
||||
}
|
||||
|
||||
fun Int?.ifNullOrZero(defaultValue: String): String {
|
||||
return if (this == null || this == 0) defaultValue else this.toString()
|
||||
}
|
||||
|
||||
fun Double?.ifNullOrZero(defaultValue: String): String {
|
||||
return if (this == null || this == 0.toDouble()) defaultValue else this.toString()
|
||||
}
|
||||
|
||||
fun View.hideKeyboard() {
|
||||
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||
imm.hideSoftInputFromWindow(this.windowToken, 0)
|
||||
this.clearFocus() // 清除焦点避免键盘再次弹出
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制文本到剪贴板
|
||||
* @param context 上下文
|
||||
* @param text 要复制的文本内容
|
||||
* @param showToast 是否显示复制成功提示,默认为true
|
||||
*/
|
||||
fun String.copyText(context: Context, showToast: Boolean = true) {
|
||||
// 获取剪贴板管理器
|
||||
val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
|
||||
// 创建ClipData对象,包含要复制的文本
|
||||
val clipData = ClipData.newPlainText("label", this)
|
||||
|
||||
// 将数据设置到剪贴板
|
||||
clipboardManager.setPrimaryClip(clipData)
|
||||
|
||||
// 显示复制成功提示
|
||||
if (showToast) {
|
||||
Toast.makeText(context, "文本已复制到剪贴板", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.sw.inbound.utils.ext
|
||||
|
||||
import android.text.SpannableStringBuilder
|
||||
import android.text.Spanned
|
||||
|
||||
|
||||
inline fun buildSpannableString(builderAction: SpannableStringBuilder.() -> Unit): SpannableStringBuilder {
|
||||
return SpannableStringBuilder().apply(builderAction)
|
||||
}
|
||||
|
||||
fun SpannableStringBuilder.appendText(text: String, vararg spans: Any): SpannableStringBuilder {
|
||||
val start = length
|
||||
append(text)
|
||||
spans.forEach { span ->
|
||||
setSpan(span, start, length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
fun SpannableStringBuilder.withSpan(span: Any, block: SpannableStringBuilder.() -> Unit) {
|
||||
val start = length
|
||||
block()
|
||||
setSpan(span, start, length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
|
||||
}
|
||||
Reference in New Issue
Block a user