如何在Kotlin中为EditText addTextChangeListener构建lambda表达式?下面给出了一个错误:
passwordEditText.addTextChangedListener { charSequence ->
try {
password = charSequence.toString()
} catch (error: Throwable) {
raise(error)
}
}
Run Code Online (Sandbox Code Playgroud) 我在我的Android项目中使用Anko,但我不知道它如何引用我在DSL中创建的子视图,当引用的视图不在我引用它的同一级别时.
以下代码有效:
alert {
customView {
val input = textInputLayout {
editText {
hint = "Name"
textColor =resources.getColor(R.color.highlight)
}
}
positiveButton("OK") { "${input.editText.text}" }
}
}.show()
Run Code Online (Sandbox Code Playgroud)
但以下代码不起作用:
alert {
customView {
val vertical = verticalLayout {
textView {
text = "Edit device name"
textColor = resources.getColor(R.color.highlight)
textSize = 24F
}
val input = textInputLayout {
editText {
hint = "Name"
textColor = resources.getColor(R.color.highlight)
}
}
}
positiveButton("OK") { "${vertical.input.editText.text}" } // Cannot resolve "input"
}
}.show()
Run Code Online (Sandbox Code Playgroud) 在Github上wiki页面中使用显示这个例子Activity
比如:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
verticalLayout {
padding = dip(30)
editText {
hint = "Name"
textSize = 24f
}
editText {
hint = "Password"
textSize = 24f
}
button("Login") {
textSize = 26f
}
}
}
Run Code Online (Sandbox Code Playgroud)
怎么做同样的内心Fragment
?
我试图将该verticalLayout
块放入onCreateView
但该方法无法解决.我增加了anko-support-v4
依赖性,但仍然没有运气.
我想创建一个自定义视图,它只是一些Android视图的包装器.我研究了创建一个自定义ViewGroup来管理它的子视图的布局,但我不需要这样的复杂性.我基本上想要做的是:
class MainActivity
verticalLayout {
textView {
text = "Something that comes above the swipe"
}
swipeLayout {
}
}
class SwipeLayout
linearLayout {
textView {
text = "Some text"
}
textView {
text = "Another text"
}
}
Run Code Online (Sandbox Code Playgroud)
原因是我想将SwipeLayout代码移动到一个单独的文件中,但不希望自己做任何复杂的布局.这可能使用Anko吗?
编辑:正如所建议的,如果视图是根布局,是否可以在Kotlin Anko中重用布局来解决此问题.但是如示例所示,我想将其包含在另一个布局中.那可能吗?
horizontalLayout
在anko/kotlin 做一个好方法是什么?verticalLayout
工作正常 - 可以设置方向,但感觉不对.不知道我在那里失踪了什么.
和anko一样,你可以写这样的回调函数:
alert {
title = ""
message = ""
yesButton {
toast("Yes")
}
noButton {
toast("No")
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何创建这样的嵌套函数?我尝试像下面那样创建它,但似乎没有工作.
class Test {
fun f1(function: () -> Unit) {}
fun f2(function: () -> Unit) {}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我用扩展功能,
fun Context.temp(function: Test.() -> Unit) {
function.onSuccess() // doesn't work
}
Run Code Online (Sandbox Code Playgroud)
从活动中调用此内容:
temp {
onSuccess {
toast("Hello")
}
}
Run Code Online (Sandbox Code Playgroud)
不行.我在这里仍然缺乏一些基本概念.谁能在这里指导?
我对使用Android KTX和Anko的优点和缺点感到有些困惑.因为两个库都试图实现相同的最终目标,并且它们之间的界限变得有点模糊,在某些情况下,两个库中都可以使用相同的功能.
如果您帮助我澄清每个人的任务及其使用案例,我将非常感激.
提前致谢!
在Kotlin中,当一个类有多个构造函数时,我们如何在另一个构造函数中调用指定的(来自iOS世界,我找不到更好的名称)构造函数.
让我举个例子
final class LoadingButton: LinearLayout {
var text:String? = null
constructor(context: Context, text: String) : this(context) {
this.text = text
}
constructor(context: Context) : super(context) {
val t = this.text
this.view {
button(t) { /* ... */}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里,如果我这样做loadingButton("TEST", {})
,那个字符串不会传播到按钮,因为this(context)
在方便构造函数中的代码之前调用(再次抱歉:).
可以在Kotlin解决吗?就像是
constructor(context: Context, text: String) {
this.text = text
this(context)
}
Run Code Online (Sandbox Code Playgroud)
编辑
只是为了澄清这个想法,因为它被问到,想法是在一个活动中写这样的东西:
onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
verticalLayout {
loadingButton("Some text")
//or
loadingButton() { this.text = "Some text"}
} …
Run Code Online (Sandbox Code Playgroud) 当我从Android片段调用toast("Toast的消息文本")时,我遇到了以下错误:
java.lang.NoSuchMethodError:没有虚方法getActivity()Landroid/app/Activity; 在课堂上Landroid/support/v4/app/Fragment; 或其超类('android.support.v4.app.Fragment'的声明出现在name-of-the-files-classes.dex中)
我正在使用Anko v0.9.1和Kotlin 1.0.6
可能是造成这次事故的原因是什么?标准Android Toast工作得很好.toast()函数也可以在Activities中运行.
我曾经使用Channel
过将点击事件从 Anko View 类发送到 Activity 类,但是越来越多的Channel
功能被标记为已弃用。所以我想开始使用Flow
apis。
我迁移了下面的代码:
private val btnProduceChannel = Channel<Unit>()
val btnChannel : ReceiveChannel<Unit> = btnProduceChannel
// Anko
button {
onClick {
btnProduceChannel.send(Unit)
}
}
Run Code Online (Sandbox Code Playgroud)
到:
lateinit var btnFlow: Flow<Unit>
private set
button {
btnFlow = flow {
onClick {
emit(Unit)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我必须像var
现在一样标记流属性,这不像以前那么优雅。这种方式对吗?我可以初始化一个的RxSubject
像Flow
定义属性是什么时候?
编辑:
我带Channel
回来,然后使用consumeAsFlow()
:
private val btnChannel = Channel<Unit>()
// This can be collected only once
val btnFlow = …
Run Code Online (Sandbox Code Playgroud)