android - 第一个代码和第二个代码之间有什么区别,为什么我应该添加getString?

标签 android kotlin

第一个带有resources.getString的代码

  btn_w.setOnClickListener {  Toast.makeText(this ,resources.getString(R.string.app_name) ,Toast.LENGTH_SHORT).show() }

没有资源的第二个代码
 btn_w.setOnClickListener {  Toast.makeText(this ,R.string.app_name ,Toast.LENGTH_SHORT).show() }

最佳答案

代码的第一行根据您的资源ID查找适当的字符串(例如R.string.app_name的字符串资源可能是“我的应用程序”)。

第二行代码仅包含您字符串的“资源ID”。该ID是一个可用于查找实际字符串的数字(例如R.string.app_name的资源ID可能是1234567890)。

documentation on app resources, specifically resource IDs可能有用。

关于android - 第一个代码和第二个代码之间有什么区别,为什么我应该添加getString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61666561/

相关文章:

java - 在 ConnectableObservable 中执行异步查询的线程问题

android - 不继承ListFragment的ListView

Kotlin 类型推断失败 - 类型不匹配 "Found Array<*?>, Required Array<*>?"

android - ACRA 4.9.0 : How can I write ACRA report to file (in Application data folder)

android - 无法使用之前设置的按钮从 customAdapter 更改 textview 值,为什么?

Android 在后台倒数计时器

android - 在 android 中使用 "by viewModels()"与 "ViewModelProvider(this).get(ViewModel::class.java)"查看模型初始化

android - fragment 内具有Tablayout的ViewPager2在kotlin中没有滑动效果

android - 协程在取消时注销接收者

android - 将我的 EditText 输入转换为可观察流