英文:
How to create onClick method for multiple buttons using View Binding in Android studio?
问题
这很容易通过view.getId()
创建onClick
事件:
public void buttonOnClick(View view)
{
switch(view.getId())
{
case R.id.button1:
// 按钮1点击的代码
break;
case R.id.button2:
// 按钮2点击的代码
break;
case R.id.button3:
// 按钮3点击的代码
break;
}
}
但是否可以使用视图绑定来创建它,或者是否可以为每个视图实现监听器呢?
P.S. 通过视图绑定,我的意思是使用一种新的视图声明方式:
private ResultProfileBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ResultProfileBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
}
英文:
This is simple to create onClick through view.getId()
public void buttonOnClick(View view)
{
switch(view.getId())
{
case R.id.button1:
// Code for button 1 click
break;
case R.id.button2:
// Code for button 2 click
break;
case R.id.button3:
// Code for button 3 click
break;
}
}
But is it possible to create it using view binding or I may as well implement listeners for each view instead?
P.S By view binding I mean using a new way of declaration of views
private ResultProfileBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ResultProfileBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
}
答案1
得分: 1
TD LR: 为每个视图实现监听器。
从Android文档中可以看到,视图绑定实际上是为您的绑定布局创建一个类。这意味着,如果您有一个包含3个文本视图的布局,您的绑定类将有3个属性,以其ID命名:
- textview1
- textview2
- textview3
每个属性已经转换为正确的类型(textView)。基本上消除了您上面发布的代码的需要,因为如果您仔细考虑,您所做的是获取视图的ID,然后进行转换,但正如我所说,绑定类已经为您完成了所有这些工作。
然而,如果您想要在多个视图中实现相同的代码,我建议将共享代码抽象到一个单独的函数中,并将该函数设置为视图的点击监听器。
binding.textview1.setOnClickListener { sharedFunction() }
binding.textview2.setOnClickListener { sharedFunction() }
binding.textview3.setOnClickListener { sharedFunction() }
英文:
TD LR implement listeners for each view instead
From the Android docs here you will see that what view binding actually does is to create a class for your bind layout. That means that if you have a layout with 3 textviews for instance, your binding class will have 3 properties named after their ids
- textview1
- textview2
- textview3
each of which is already cast to the correct type (textView). basically eliminating the need for doing the code you post above, because if you think about it, what you are doing is getting the id of the view and the you do the cast but as I said the binding class has already done all that work for you.
however if what you are trying to do is implement the same code in multiple views then I will suggest to abstract the shared code to a separate function and set that function on the views click listener.
binding.textview1.setOnClickListener { sharedFunction() }
binding.textview2.setOnClickListener { sharedFunction() }
binding.textview3.setOnClickListener { sharedFunction() }
答案2
得分: 0
你可以使用Butterknife库。
查看该库:https://github.com/JakeWharton/butterknife
英文:
You can use Butterknife library
check this library=https://github.com/JakeWharton/butterknife
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论