I am learning Android from a few tutorials, and when it comes to using OnClickListeners, I notice that a lot of them use anonymous inner classes like so:
public class MainActivity extends AppCompatActivity {
private EditText mNameField;
private Button mStartButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameField = (EditText) findViewById(R.id.nameEditText);
mStartButton = (Button) findViewById(R.id.startButton);
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mNameField.getText().toString();
Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
}
});
}
}
Is this considered good practice to have a class inside a class? Or is it considered better to somehow define this class outside somewhere and then reference it? How would I do this?