如何在Android Studio中使用View Binding为多个按钮创建onClick方法?

huangapple go评论103阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年9月3日 01:06:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63710349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定