【问题标题】:What is better - one long onClick method for all buttons, or many short methods for each button[android]什么更好 - 所有按钮的长 onClick 方法,或每个按钮的许多短方法[android]
【发布时间】:2015-10-02 04:28:13
【问题描述】:

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

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

点击/触摸发生后,我的应用需要花费太多时间来渲染图像 - 大约 2-3 秒。我启动了 ddms 以查看发生了什么,进行了跟踪,并且我的应用程序偶然发现了 goToCategory() - 至少我认为这是导致渲染长时间延迟的根本问题。我可能想重写 long 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 ;

    }
}

【问题讨论】:

  • 无论哪种方式都不会有明显的性能差异,尤其是对于这种很少被调用的方法。

标签: android performance onclick dalvik ddms


【解决方案1】:

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

找到相关话题:

1) Best practice for defining button events in android

2) best practices for handling UI events

【讨论】:

  • 这个答案应该是评论
  • 这不应该被评论,因为这是一个正确的答案格式。
  • 实际上,我已经有了 onClick 方法,我在问是否有很多单独的 onClick 比一个巨大的 onClick 方法更好
【解决方案2】:

XML 布局中带有函数绑定的android:onClickonClick之间的绑定 以及它将调用的函数。该函数必须有一个参数(视图)才能使 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?

【讨论】:

    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多