英文:
Should I write clickListeners inside onCreate() method or should I create a separate method with onClick attribute in XML file in android studio?
问题
I am currently taking Android dev classes and working on a small app. I have written my code with separate methods for the clickListeners outside my onCreate
method like below:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void numberActivity(View view) {
TextView textView = (TextView)findViewById(R.id.numbers);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"Numbers", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(view.getContext(), NumbersActivity.class);
startActivity(intent);
}
});
}
However, I saw in the sample code from the lessons that the code was written inside onCreate()
method like below:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView numbers = (TextView) findViewById(R.id.numbers);
numbers.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);
startActivity(numbersIntent);
}
});
}
}
So, my question is which way is more correct? In either case, the app is launching and the listeners are working. I would like to know which way is better for the performance and speed of the app.
英文:
I am currently taking Android dev classes and working on a small app. I have written my code with separate methods for the clickListeners outside my onCreate
method like below:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void numberActivity(View view) {
TextView textView = (TextView)findViewById(R.id.numbers);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"Numbers", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(view.getContext(), NumbersActivity.class);
startActivity(intent);
}
});
}
However, I saw in the sample code from the lessons that the code was written inside onCreate()
method like below:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView numbers = (TextView) findViewById(R.id.numbers);
numbers.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);
startActivity(numbersIntent);
}
});
So, my question is which way is more correct? In either cases the app is launching and the listeners are working. I would like to know which way is better for the performance and speed of the app.
答案1
得分: 0
在我看来,你应该在你的Java代码中添加所有的onClickListeners,而不是在XML中。然而,我认为将它们添加到onCreate或者在onCreate中调用的方法中,对性能来说完全没有关联。这只是一个关于哪种方式对你更方便和/或更容易阅读和理解的问题。
英文:
In my opinion you should add all your onClickListeners in your java code and not in xml.
However I think it is completely irrelevant for performance if you add them in onCreate or a method you call in onCreate. This is only a question of what is more convenient for you and/or better to read and understand.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论