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.