简体   繁体   English

实现OnClickListener VS. new Button.OnClickListener(){};

[英]implements OnClickListener VS. new Button.OnClickListener() {};

I have a question about implementing OnClickListeners for developing with the ADT. 我有一个关于实现OnClickListeners以使用ADT进行开发的问题。 I'm unsure of which way is more efficient, can anyone please provide me with pro's and con's of each approach? 我不确定哪种方式更有效率,任何人都可以请我提供每种方法的专业知识吗?

class x extends Activity implements OnClickListener
{
  button.SetOnClickListener(this);
  OnclickListener(View v)
  {
    switch(v.getGetId());
    {
      case R.id.y:
      //do stuff here
      break;
      .
      .
      .
    }
  }

}

<-VERSUS-> <-VERSUS->

class a extends Activity
{
   .
   .
   .
   btn.setOnClickListener(new Button.OnClickListener()
   {

    OnClickListener(View v)
    {
      //do stuff here
    }

   });

}

I think its mostly a case of personal preference. 我认为这主要是个人偏好的情况。 Any performance difference is likely going to be negligible. 任何性能差异都可能微不足道。

Personally, I prefer the nested class: 就个人而言,我更喜欢嵌套类:

  1. Its harder to screw up 搞砸更难
  2. Switch statements are ugly 切换语句很难看
  3. You can make use of local variables that may be useful 您可以使用可能有用的局部变量

But some people think that nested classes are ugly, and so prefer the implements approach. 但是有些人认为嵌套类是丑陋的,所以更喜欢实现方法。 That approach does work better if you only have one listener implemented in the activity. 如果您只在活动中实现了一个侦听器,那么该方法可以更好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM