如何用视图绑定替换 findViewById(v.getId())?
How to replace findViewById(v.getId()) with View Binding?
通过点击一个按钮,我可以通过findViewById(v.getId())
找到按钮的id。如何将其替换为视图绑定?
这是我的代码:
fun TastoClick(v: View) {
val btn = findViewById(v.getId()) as Button
var name = btn.text.toString().toInt()
}
请看一下 View Binding 的文档,我相信您会发现它可以回答您的问题。
但总而言之:
在模块级别build.gradle添加:
android {
...
buildFeatures {
viewBinding true
}
}
对于具有 xml 的 片段,例如 result_profile.xml
,将以以下格式生成绑定 class: ResultProfileBinding
然后您需要为 class 设置一个实例,如下所示:
result_profile.xml:
<LinearLayout ... >
<TextView android:id="@+id/name" />
<ImageView android:cropToPadding="true" />
<Button android:id="@+id/button"
android:background="@drawable/rounded_button" />
</LinearLayout>
ResultProfileFragment.kt:
private var _binding: ResultProfileBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = ResultProfileBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
- 对于Activity:
ResultProfileActivity.kt:
private lateinit var binding: ResultProfileBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ResultProfileBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
}
然后您可以像这样访问布局元素:
binding.name.text = viewModel.name
binding.button.setOnClickListener { viewModel.userClicked() }
更新:
如果您有多个按钮的一个侦听器,您可以简单地将传递到 onClick
方法的视图与您的视图绑定 ID 进行比较,即
v.id == binding.yourbutton.id
switch 语句或 if 语句可用于检查哪个 ID 与被单击的视图匹配。
科特林
val btn = binding.root.findViewById<Button>(v.id)
通过点击一个按钮,我可以通过findViewById(v.getId())
找到按钮的id。如何将其替换为视图绑定?
这是我的代码:
fun TastoClick(v: View) {
val btn = findViewById(v.getId()) as Button
var name = btn.text.toString().toInt()
}
请看一下 View Binding 的文档,我相信您会发现它可以回答您的问题。
但总而言之:
在模块级别build.gradle添加:
android { ... buildFeatures { viewBinding true } }
对于具有 xml 的 片段,例如
result_profile.xml
,将以以下格式生成绑定 class:ResultProfileBinding
然后您需要为 class 设置一个实例,如下所示:
result_profile.xml:
<LinearLayout ... >
<TextView android:id="@+id/name" />
<ImageView android:cropToPadding="true" />
<Button android:id="@+id/button"
android:background="@drawable/rounded_button" />
</LinearLayout>
ResultProfileFragment.kt:
private var _binding: ResultProfileBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = ResultProfileBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
- 对于Activity:
ResultProfileActivity.kt:
private lateinit var binding: ResultProfileBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ResultProfileBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
}
然后您可以像这样访问布局元素:
binding.name.text = viewModel.name
binding.button.setOnClickListener { viewModel.userClicked() }
更新:
如果您有多个按钮的一个侦听器,您可以简单地将传递到 onClick
方法的视图与您的视图绑定 ID 进行比较,即
v.id == binding.yourbutton.id
switch 语句或 if 语句可用于检查哪个 ID 与被单击的视图匹配。
科特林
val btn = binding.root.findViewById<Button>(v.id)