在我目前的android项目中,我必须使用Litho框架。我现在面临的问题是如何使用Litho EditText小部件处理输入键。(按下软键盘上的输入键时调用请求) 我在LithoView上尝试了setOnKeyListener,它包含EditText但没有任何效果。因此,如果有人使用Litho和EditText,可以帮助我高兴。
答案 0 :(得分:0)
它会触发点击事件,不幸的是,这也会导致定期点击。
import android.graphics.Color
import android.text.InputType
import android.util.Log
import com.facebook.litho.*
import com.facebook.litho.annotations.*
import com.facebook.litho.widget.EditText
import com.facebook.litho.widget.Recycler
import com.facebook.litho.widget.RecyclerBinder
import com.facebook.litho.widget.TextChangedEvent
import com.facebook.yoga.YogaEdge
interface OnExecuteListener {
fun onExecute(query: String)
}
@LayoutSpec
class MainComponentSpec {
companion object {
@JvmStatic
@OnCreateInitialState
fun createInitialState(c: ComponentContext, url: StateValue<String>, @Prop initialUrl: String) {
url.set(initialUrl)
}
@JvmStatic
@OnCreateLayout
fun onCreateLayout(
c: ComponentContext, @State url: String, @Prop recyclerBinder: RecyclerBinder): ComponentLayout {
return Column.create(c)
.paddingDip(YogaEdge.ALL, 16f)
.backgroundColor(Color.WHITE)
.child(urlText(c, url))
.child(getRecyclerComponent(c, recyclerBinder))
.build()
}
@JvmStatic
@OnEvent(TextChangedEvent::class)
fun onTextChanged(c: ComponentContext, @State url: String) {
Log.i("X", "tc: " + url)
}
@JvmStatic
@OnEvent(ClickEvent::class)
fun onClick(c: ComponentContext, @State url: String, @Prop executeListener: OnExecuteListener) {
executeListener.onExecute(url)
}
private fun urlText(c: ComponentContext, url: String): EditText.Builder? {
return EditText.create(c)
.text(url)
.textSizeSp(14f)
.isSingleLine(true)
.inputType(InputType.TYPE_TEXT_VARIATION_URI)
.clickHandler(MainComponent.onClick(c))
.textChangedEventHandler(MainComponent.onTextChanged(c))
}
private fun getRecyclerComponent(c: ComponentContext,
recyclerBinder: RecyclerBinder): Component<Recycler> {
return Recycler.create(c).binder(recyclerBinder).build()
}
}
}