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

时间:2015-10-02 04:28:13

标签: android performance onclick dalvik ddms

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

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

我的应用程序在点击/触摸发生后渲染图像需要花费太多时间 - 大约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 ;

    }
}

2 个答案:

答案 0 :(得分:1)

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

找到相关主题:

1)Best practice for defining button events in android

2)best practices for handling UI events

答案 1 :(得分:1)

XML布局 中使用功能绑定的 android:onClick 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?