有什么方法可以通过 kotlin-extensions 更改自动创建的视图的名称?
Any way to change names for auto-created views by kotlin-extensions?
我是 Kotlin 新手。我已将 https://kotlinlang.org/docs/tutorials/android-plugin.html 设为红色,并注意到可以通过导入将视图自动绑定到 activity
kotlinx.android.synthetic.main.activity_main.*.
如果我在 Activity 中使用 id = "btn_login" 声明视图,我可以通过
访问它
activity.btn_login.setText("Login")
但是。有什么方法可以更改别名以查看,例如 ButterKnife 所做的:
@BindView(<id of view>)
<name of view>
这是一个合成导入,所以从技术上讲,您可以使用导入别名来调用它的另一个名称:
import kotlinx.android.synthetic.main.activity_main.view.btn_login as btnLogin
但考虑到没有工具可以自动执行此操作,您可能只想采用不同的 ID 命名方案。
这是一个合理的:
What-Where-Description-Modifier:
recyclerSearchSuggestions
- RecyclerView showing search suggestions
fabSearchGo
- FloatingActionButton that executes a search
textSearchFilterChip
- TextView that represents search filters, styled as a material chip
buttonSearchClearFilter
- Button that clears selected filter chips
editSearchFilter
- EditText used to narrow down search suggestions
据我所知,唯一可行的方法是使用命名导入。这是 Kotlin 语言的一个非常好的特性,Java 不支持。不幸的是,这需要为那些你想替换的人手动设置它,所以它可能有点样板。
但您可以将导入更改为:
import kotlinx.android...your_view as yourView
这适用于任何和所有导入,也适用于任何类型。可以用 类、方法、常量……随心所欲。
尽管如果您可以访问 XML 文件,我建议您只更改其中的 ID。没有理由不使用 camelCase,而且它比在每次导入时使用 as customName
稍微容易一些。
我是 Kotlin 新手。我已将 https://kotlinlang.org/docs/tutorials/android-plugin.html 设为红色,并注意到可以通过导入将视图自动绑定到 activity
kotlinx.android.synthetic.main.activity_main.*.
如果我在 Activity 中使用 id = "btn_login" 声明视图,我可以通过
访问它activity.btn_login.setText("Login")
但是。有什么方法可以更改别名以查看,例如 ButterKnife 所做的:
@BindView(<id of view>)
<name of view>
这是一个合成导入,所以从技术上讲,您可以使用导入别名来调用它的另一个名称:
import kotlinx.android.synthetic.main.activity_main.view.btn_login as btnLogin
但考虑到没有工具可以自动执行此操作,您可能只想采用不同的 ID 命名方案。
这是一个合理的:
What-Where-Description-Modifier:
recyclerSearchSuggestions
- RecyclerView showing search suggestions
fabSearchGo
- FloatingActionButton that executes a search
textSearchFilterChip
- TextView that represents search filters, styled as a material chip
buttonSearchClearFilter
- Button that clears selected filter chips
editSearchFilter
- EditText used to narrow down search suggestions
据我所知,唯一可行的方法是使用命名导入。这是 Kotlin 语言的一个非常好的特性,Java 不支持。不幸的是,这需要为那些你想替换的人手动设置它,所以它可能有点样板。
但您可以将导入更改为:
import kotlinx.android...your_view as yourView
这适用于任何和所有导入,也适用于任何类型。可以用 类、方法、常量……随心所欲。
尽管如果您可以访问 XML 文件,我建议您只更改其中的 ID。没有理由不使用 camelCase,而且它比在每次导入时使用 as customName
稍微容易一些。