Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCheen committed Nov 5, 2021
1 parent 312ce56 commit 15abfac
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class SpannableStringBuilderDSL : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

"SpannableStringBuilderDSL test case".toSpannableDSL {
"SpannableStringBuilderDSL test case".toSpannableStringDSL {
bold(0,"SpannableStringBuilderDSL")
foregroundColor(Color.RED,10,15)
foregroundColor(Color.RED,"case")
backgroundColor(Color.BLUE,"test")
italic(0,"able")
}.bindTo(view.findViewById(R.id.result))
}.applyTo(view.findViewById(R.id.result))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import android.widget.TextView
* KTX for CharSequence
*/

fun CharSequence.toSpannable(): SpannableString {
fun CharSequence.toSpannableString(): SpannableString {
return SpannableString(this)
}

fun CharSequence.toSpannableDSL(builder: SpannableString.() -> Unit): SpannableString {
fun CharSequence.toSpannableStringDSL(builder: SpannableString.() -> Unit): SpannableString {
return SpannableString(this).apply(builder)
}

fun CharSequence.bindTo(textView: TextView) {
fun CharSequence.applyTo(textView: TextView) {
textView.text = this
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import androidx.fragment.app.Fragment
* Created by 程序亦非猿 on 2021/4/12.
*/

fun Fragment?.toast(text: CharSequence?, duration: Int = Toast.LENGTH_SHORT) {
fun Fragment?.showToast(text: CharSequence?, duration: Int = Toast.LENGTH_SHORT) {
if (this == null || text == null || this.activity == null) {
return
}
Toast.makeText(activity, text, duration).show()
}

fun Fragment?.toast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
fun Fragment?.showToast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
if (this == null || this.activity == null) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fun <T> Any.runIfIs(clazz: Class<T>, block: T.() -> Unit) {

/**
*
* 为什么不用 Looper.myLooper() == Looper.getMainLooper()? 因为 myLooper() 可能会创建 ThreadLocalMap 导致浪费
* 为什么不用 Looper.myLooper() == Looper.getMainLooper()? 因为 myLooper() 可能会创建 ThreadLocalMap 导致浪费,而用 Thread 判断不需要
*/
fun isOnMainThread() :Boolean{
return Looper.getMainLooper().thread == Thread.currentThread()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ fun SpannableStringBuilder.foregroundColor(
setSpan(ForegroundColorSpan(color), start, start + text.length, flag)
}

fun SpannableStringBuilder.bindTo(textView: TextView) {
fun SpannableStringBuilder.applyTo(textView: TextView) {
textView.text = this
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.widget.EditText
* Created by 程序亦非猿 on 2021/9/13.
*/

fun EditText.onTextChanged(func: (s: CharSequence, start: Int, before: Int, count: Int) -> Unit) {
fun EditText.doOnTextChanged(func: (s: CharSequence, start: Int, before: Int, count: Int) -> Unit) {
addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
Expand All @@ -22,7 +22,7 @@ fun EditText.onTextChanged(func: (s: CharSequence, start: Int, before: Int, coun
})
}

fun EditText.beforeTextChanged(func: (s: CharSequence, start: Int, count: Int, after: Int) -> Unit) {
fun EditText.doBeforeTextChanged(func: (s: CharSequence, start: Int, count: Int, after: Int) -> Unit) {
addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
func(s, start, count, after)
Expand All @@ -36,7 +36,7 @@ fun EditText.beforeTextChanged(func: (s: CharSequence, start: Int, count: Int, a
})
}

fun EditText.afterTextChanged(func: (text: String, length: Int, isEmpty: Boolean, s: Editable) -> Unit) {
fun EditText.doAfterTextChanged(func: (text: String, length: Int, isEmpty: Boolean, s: Editable) -> Unit) {
addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
package me.yifeiyuan.onepiece.pandora.ktx.ui

import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat

/**
* Created by 程序亦非猿 on 2021/10/12.
*/

fun TextView.isTextNullOrEmpty(): Boolean {
return text.isNullOrEmpty()
}

fun TextView.getTextOrEmpty(): CharSequence {
return text ?: ""
}

fun <T : TextView> T.textColor(resId: Int): T {
fun <T : TextView> T.setTextColorRes(@ColorRes resId: Int): T {
setTextColor(ContextCompat.getColor(context, resId))
return this
}

fun <T : TextView> T.text(resId: Int): T {
fun <T : TextView> T.setTextRes(@StringRes resId: Int): T {
text = context.resources.getString(resId)
return this
}

fun <T : TextView> T.setTextColorByCondition(condition: Boolean, trueColor: Int, falseColor: Int) {
val colorRes = if (condition) trueColor else falseColor
setTextColor(ContextCompat.getColor(context, colorRes))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.viewpager.widget.ViewPager
* Created by 程序亦非猿 on 2021/9/13.
*/

fun ViewPager.onPageScrolled(
fun ViewPager.doOnPageScrolled(
func: (
position: Int,
positionOffset: Float,
Expand Down

0 comments on commit 15abfac

Please sign in to comment.