add ListenerExt
This commit is contained in:
parent
8a237a1156
commit
9c799fa8d4
|
|
@ -33,6 +33,7 @@
|
|||
<activity android:name="luyao.util.ktx.ui.SharedPreferencesActivity"/>
|
||||
<activity android:name="luyao.util.ktx.ui.ActivityExtActivity"/>
|
||||
<activity android:name="luyao.util.ktx.ui.AnotherActivity"/>
|
||||
<activity android:name="luyao.util.ktx.ui.ListenerExtActivity"/>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
|
@ -16,6 +16,7 @@ class ExtActivity : CommonListActivity() {
|
|||
add(ItemBean("ToastExt", ToastExtActivity::class.java))
|
||||
add(ItemBean("AppExt", ToastExtActivity::class.java))
|
||||
add(ItemBean("IntentExt", IntentExtActivity::class.java))
|
||||
add(ItemBean("ListenerExtActivity", ListenerExtActivity::class.java))
|
||||
add(ItemBean("PermissionExt",PermissionExtActivity::class.java))
|
||||
add(ItemBean("SharedPreferencesExt",SharedPreferencesActivity::class.java))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
package luyao.util.ktx.ui
|
||||
|
||||
import kotlinx.android.synthetic.main.activity_listener.*
|
||||
import kotlinx.android.synthetic.main.title_layout.*
|
||||
import luyao.ktx.R
|
||||
import luyao.util.ktx.base.BaseActivity
|
||||
import luyao.util.ktx.ext.textWatcher
|
||||
import luyao.util.ktx.ext.toast
|
||||
|
||||
/**
|
||||
* Created by luyao
|
||||
* on 2019/7/9 16:24
|
||||
*/
|
||||
class ListenerExtActivity : BaseActivity() {
|
||||
|
||||
override fun getLayoutResId() = R.layout.activity_listener
|
||||
|
||||
override fun initView() {
|
||||
mToolbar.title = "ListenerExtActivity"
|
||||
}
|
||||
|
||||
override fun initData() {
|
||||
listenerEt.textWatcher {
|
||||
onTextChanged { s, start, before, count ->
|
||||
toast(s.toString())
|
||||
}
|
||||
|
||||
afterTextChanged { toast(it.toString()) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/light_gray">
|
||||
|
||||
<include layout="@layout/title_layout"/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatEditText
|
||||
android:id="@+id/listenerEt"
|
||||
style="@style/NormalEditText"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
|
@ -14,4 +14,23 @@
|
|||
<item name="android:textColor">@color/black</item>
|
||||
<item name="android:background">@drawable/ripple</item>
|
||||
</style>
|
||||
|
||||
<style name="NormalEditText">
|
||||
<item name="android:layout_width">match_parent</item>
|
||||
<item name="android:layout_height">wrap_content</item>
|
||||
<item name="android:textAllCaps">false</item>
|
||||
<item name="android:padding">2dp</item>
|
||||
<item name="android:minHeight">40dp</item>
|
||||
<item name="android:layout_marginBottom">5dp</item>
|
||||
<item name="android:layout_marginTop">5dp</item>
|
||||
<item name="android:layout_marginLeft">10dp</item>
|
||||
<item name="android:layout_marginRight">10dp</item>
|
||||
<item name="android:textSize">16sp</item>
|
||||
<item name="android:textColor">@color/black</item>
|
||||
<item name="android:background">@color/white</item>
|
||||
<item name="android:focusable">true</item>
|
||||
<item name="android:focusableInTouchMode">true</item>
|
||||
<item name="android:hint">Please Input</item>
|
||||
<item name="android:cursorVisible">true</item>
|
||||
</style>
|
||||
</resources>
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
package luyao.util.ktx.ext
|
||||
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.widget.TextView
|
||||
|
||||
/**
|
||||
* Created by luyao
|
||||
* on 2019/7/9 16:13
|
||||
*/
|
||||
|
||||
fun TextView.textWatcher(watcher: KtxTextWatcher.() -> Unit) =
|
||||
addTextChangedListener(KtxTextWatcher().apply(watcher))
|
||||
|
||||
class KtxTextWatcher : TextWatcher {
|
||||
|
||||
private var _afterTextChanged: ((Editable?) -> Unit)? = null
|
||||
private var _beforeTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
|
||||
private var _onTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
|
||||
|
||||
|
||||
fun afterTextChanged(listener: ((Editable?) -> Unit)) {
|
||||
_afterTextChanged = listener
|
||||
}
|
||||
|
||||
fun beforeTextChanged(listener: (CharSequence?, Int, Int, Int) -> Unit) {
|
||||
_beforeTextChanged = listener
|
||||
}
|
||||
|
||||
fun onTextChanged(listener: (CharSequence?, Int, Int, Int) -> Unit) {
|
||||
_onTextChanged = listener
|
||||
}
|
||||
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
_afterTextChanged?.invoke(s)
|
||||
}
|
||||
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
||||
_beforeTextChanged?.invoke(s, start, count, after)
|
||||
}
|
||||
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||
_onTextChanged?.invoke(s, start, before, count)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue