在DataBinding中,是否必须创建内部类来处理事件?

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

Is it mandatory to create inner classes to handle events in DataBinding?

问题

我正在学习数据绑定,特别是关于处理事件和点击事件的部分。现在,我注意到在一些YouTube教程中,讲师们主要使用内部类来处理这些事件。然而,之前我编写了这段代码,实现了View.OnClickListener,并直接允许我处理点击事件。

这是代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 
    private ActivityMainBinding activityMainBinding;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        activityMainBinding.enterButton.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View view) {
        if (view == activityMainBinding.enterButton) {
            String name = activityMainBinding.nameEditText.getText().toString();
            String email = activityMainBinding.emailEditText.getText().toString();
            String country = activityMainBinding.countryEditText.getText().toString();
 
            User user = new User(name, email, country);
            activityMainBinding.setUser(user);
        }
    }
}

这段代码有效。

我在想,这种处理点击事件的方式是否不被视为良好的实践?我查阅了更多的教程,它们都使用了内部类,因此产生了这种疑虑。

谢谢任何帮助。

英文:

I was learning about DataBinding, particularly the section about handling events and click events. Now, I noticed in a few YouTube tutorials that I instructors were mainly using an inner class to handle these events. However, earlier, I'd written this code that implemented the View.OnClickListener and directly allowed me to handle click events.

Here it is:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 
    private ActivityMainBinding activityMainBinding;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        activityMainBinding.enterButton.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View view) {
        if (view == activityMainBinding.enterButton) {
            String name = activityMainBinding.nameEditText.getText().toString();
            String email = activityMainBinding.emailEditText.getText().toString();
            String country = activityMainBinding.countryEditText.getText().toString();
 
            User user = new User(name, email, country);
            activityMainBinding.setUser(user);
        }
    }
}

And this works.

I was wondering, is this form of handling click events not considered a good practice? I checked through a few more tutorials and they all used inner classes, thereby causing this doubt.

Thanks for any help.

答案1

得分: 2

Q: 创建内部类是必要的吗?

A: 不,绝对不是。这只是一个有用的约定 在DataBinding中,是否必须创建内部类来处理事件?

Q: 这种处理点击事件的形式...是一种好的做法吗?

A: 一般来说,任何单独的“类”都应该只做“一件事”。类的属性和方法应该与类的“抽象”相匹配。

例如,一个“汽车”类可能不应该有一个“onClick()”方法。即使您的“汽车”类实现可能有一个带有“onClick()”方法的“按钮”。

或者您的“汽车”可能有一打不同的按钮。在这种情况下,我绝对更喜欢看到一打匿名内部类,每个按钮一个。这会更简洁,更清晰。

然而,在您的示例中,我没有看到任何问题。看起来很好 在DataBinding中,是否必须创建内部类来处理事件?

英文:

Q: Is it necessary to create inner classes?

> A: No, absolutely not. It's merely a useful convention 在DataBinding中,是否必须创建内部类来处理事件?

Q: Is this form of handling click events ... a good practice?

> A: In general, any individual "class" should do "one thing". The class's properties and its methods should match the class's "abstraction".
>
> For example, an "Automobile" class should probably not have an "onClick()" method. Even if your "Automobile" class implementation might have a "button", with an "onClick()" method.
>
> Or your "Automobile" might have a dozen different buttons. In that case, I'd definitely prefer to see a dozen anonymous inner classes, one for each button. It would be shorter; it would be cleaner.
>
>
> In your example, however, I don't see any problem. It looks fine 在DataBinding中,是否必须创建内部类来处理事件?

huangapple
  • 本文由 发表于 2020年10月6日 02:13:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64214037.html
匿名

发表评论

匿名网友

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

确定