Should I write clickListeners inside onCreate() method or should I create a separate method with onClick attribute in XML file in android studio?

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

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.

huangapple
  • 本文由 发表于 2020年8月11日 01:57:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63345562.html
匿名

发表评论

匿名网友

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

确定