使用Anko从活动访问视图 [英] Accessing views from the Activity with Anko
问题描述
我知道我可以在Anko中使用id
属性来识别视图:
I know I can use an id
attribute with Anko to identify a view:
class MainActivityUI : AnkoComponent<MainActivity> {
override fun createView(ui: AnkoContext<MainActivity>) = with(ui) {
frameLayout {
textView {
id = R.id.text
}
}
}
}
然后使用find()
函数(或使用Kotlin Android扩展程序)在Activity
中获取它:
Then obtain it in the Activity
using the find()
function (or by using Kotlin Android Extensions):
class MainActivity : AppCompatActivity() {
private val textView by lazy {
find<TextView>(R.id.text)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MainActivityUI().setContentView(this)
textView.text = "Hello World"
}
}
但是我觉得我缺少了一些东西. 自述文件唯一提及find
函数或Kotlin Android扩展的地方在标题为支持现有代码的部分中:
But I feel like I am missing something; the only place the README mentions the find
function or Kotlin Android Extensions is in the section titled Supporting Existing Code:
您不必使用Anko重写所有UI.你可以保持老 用Java编写的类.而且,如果您仍然想要(或必须) 编写Kotlin活动类,并为某些应用添加XML布局 因此,您可以使用View属性,这会使事情变得更容易:
You don't have to rewrite all your UI with Anko. You can keep your old classes written in Java. Moreover, if you still want (or have) to write a Kotlin activity class and inflate an XML layout for some reason, you can use View properties, which would make things easier:
// Same as findViewById(), simpler to use
val name = find<TextView>(R.id.name)
name.hint = "Enter your name"
name.onClick { /*do something*/ }
通过使用Kotlin Android,您可以使代码更加紧凑 扩展程序.
You can make your code even more compact by using Kotlin Android Extensions.
这使得find
函数似乎仅用于支持旧" XML代码.
Which makes it seem like the find
function is only meant for supporting "old" XML code.
所以我的问题是这个;使用id
和find
函数是使用Anko从Activity
访问View
的正确方法吗?还有更多的"Anko"处理方式吗?还是我错过了Anko的其他好处,而这些好处使从Activity
中访问View
无关紧要?
So my question is this; is using an id
along with the find
function the correct way of accessing a View
from the Activity
using Anko? Is there a more "Anko" way of handling this? Or am I missing some other benefit of Anko that makes accessing the View
from the Activity
irrelevant?
还有第二个相关问题;如果此是从Activity
访问View
的正确方法,是否可以从AnkoComponent
内部创建id
资源(即"@+id/"
)?而不是在ids.xml
文件中创建每个id
.
And a second related question; if this is the correct way of accessing a View
from the Activity
, is there a way of creating an id
resource (i.e. "@+id/"
) from within an AnkoComponent
? Rather than creating each id
in the ids.xml
file.
推荐答案
那么,为什么还要使用XML id
来定位视图?因为我们已经使用Anko而不是XML.
So, why still use XML id
to locate the View? since we already use the Anko instead of the XML.
我认为,我们可以将view元素存储在AnkoComponent
中,而不是find view
的id方法中.检查代码打击:
In my opinion, we can store the view elements inside the AnkoComponent
instead of the find view
's id method. Check the code blow:
class MainActivityUI : AnkoComponent<MainActivity> {
lateinit var txtView: TextView
override fun createView(ui: AnkoContext<MainActivity>) = with(ui) {
frameLayout {
txtView = textView {
id = R.id.text // the id here is useless, we can delete this line.
}
}
}
}
class MainActivity : AppCompatActivity() {
lateinit var mainUI : MainActivityUI
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mainUI = MainActivityUI()
mainUI.setContentView(this)
mainUI.txtView.text = "Hello World"
}
}
这篇关于使用Anko从活动访问视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!