更好的是 - 所有按钮的长 onClick 方法,或每个按钮的许多短方法 [android]

What is better - one long onClick method for all buttons, or many short methods for each button[android]

我有 12 个按钮,分为 2 组,每组有 6 个按钮,所有按钮都响应一个长的 onClick 方法 goToCategory()。

我可以将它重构为很多小的独立的onclick方法。

我的应用程序在 click/touch 发生后渲染图像需要花费太多时间 - 大约 2-3 秒。我启动了 ddms 以查看发生了什么,进行了跟踪,我的应用程序偶然发现了 goToCategory() - 至少我认为这是导致渲染长时间延迟的根本问题。我可能想重写长onclick方法。

从性能的角度来看,什么更好?

public void goToCategory(View v){
    switch (v.getId()){
        case R.id.scientists:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.galim1);
            hero2.setBackgroundResource(R.drawable.galim2);
            hero3.setBackgroundResource(R.drawable.galim3);
            upper_category_index = "science";
            break ;
        case R.id.scientists2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.galim1);
            hero5.setBackgroundResource(R.drawable.galim2);
            hero6.setBackgroundResource(R.drawable.galim3);
            lower_category_index = "science";
            break ;
        case R.id.politics:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.pol1);
            hero2.setBackgroundResource(R.drawable.pol2);
            hero3.setBackgroundResource(R.drawable.pol3);
            upper_category_index = "politics";
            break ;
        case R.id.politics2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.pol1);
            hero5.setBackgroundResource(R.drawable.pol2);
            hero6.setBackgroundResource(R.drawable.pol3);
            lower_category_index = "politics";
            break ;
        case R.id.akins:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.akin1);
            hero2.setBackgroundResource(R.drawable.akin2);
            hero3.setBackgroundResource(R.drawable.akin3);
            upper_category_index = "akin";
            break ;
        case R.id.akins2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.akin1);
            hero5.setBackgroundResource(R.drawable.akin2);
            hero6.setBackgroundResource(R.drawable.akin3);
            lower_category_index = "akin";
            break ;
    case R.id.folk_heroes:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.folk1);
            hero2.setBackgroundResource(R.drawable.folk2);
            hero3.setBackgroundResource(R.drawable.folk3);
            upper_category_index = "folk";
        break ;
        case R.id.folk_heroes2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.folk1);
            hero5.setBackgroundResource(R.drawable.folk2);
            hero6.setBackgroundResource(R.drawable.folk3);
            lower_category_index = "folk";
            break ;
        case R.id.hans:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.han1);
            hero2.setBackgroundResource(R.drawable.han2);
            hero3.setBackgroundResource(R.drawable.han3);
            upper_category_index = "hans";
            break ;
        case R.id.hans2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.han1);
            hero5.setBackgroundResource(R.drawable.han2);
            hero6.setBackgroundResource(R.drawable.han3);
            lower_category_index = "hans";
            break ;
        case R.id.batirs:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.kabanbai);
            hero2.setBackgroundResource(R.drawable.bogenbai);
            hero3.setBackgroundResource(R.drawable.karasai);
            upper_category_index = "batirs";
            break ;
        case R.id.batirs2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.kabanbai);
            hero5.setBackgroundResource(R.drawable.bogenbai);
            hero6.setBackgroundResource(R.drawable.karasai);
            lower_category_index = "batirs";
            break ;

    }
}

我认为在 xml(布局)中注册 onClick 是更好的方法。

找到相关主题:

1) Best practice for defining button events in android

2) best practices for handling UI events

android:onClickXMLLayout[=30=中绑定函数] 是 onClick 和它将调用的函数之间的绑定。该函数必须有一个参数(视图)才能使 onClick 发挥作用.

https://developer.android.com/reference/android/widget/Button.html

SO COURTESY

注册 android:onClick 是更好的方法。

How exactly does the android:onClick XML attribute differ from setOnClickListener?