有效性,Android的开关或点击监听器为每个按钮 [英] Effectivity, Android switch or click listener for each button

查看:122
本文介绍了有效性,Android的开关或点击监听器为每个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能发现的话题,也许有人可以教我一些关于Android有效性当托皮斯约为onclicklistener的按钮。

I cant found the topic, perhaps someone can teach me some about android effectivity when the topis is about onclicklistener for buttons.

可以说,我有一个页面上的10个按钮(只是举个例子,现在)
什么是最好的事情?

Lets say I got 10 buttons on a page (just an example now) What's the best thing to do?


  1. 的交换机ID为​​按钮开关?

  2. 系统onClickListener为每个按钮?

用什么方法是一个更快的,为什么?
是否有什么不同呢?

What method is the faster one, and why? Is there any different at all?

推荐答案

最佳做法是为去第一种选择:即交换机ID为​​butttons开关

根据我的经验,我建议您分配安卓的onClick具有相同的属性值,例如说:安卓的onClick =btnClicker

As per my experience, i would suggest you to assign android:onClick attribute with same value, say for example: android:onClick="btnClicker"

和现在你必须完成该项活动类中的方法一样:

And now you have to implement the same method inside the activity class as:

public void btnClicker(View v)
{
   switch(v.getId())
   { 
    case R.id.btn1:
         break;

    case R.id.btn2:
         break;

    case R.id.btn3:
         break;
   }
}

关于第二个选项:
我不preFER它,因为它增加了code行的数字,因为只是觉得你有10个按钮,你分配一个单独的点击监听器为每个按钮。而现在它与上述第一种选择相比,你就会意识到这一点。

About 2nd option: I don't prefer it because it increase number of code lines because just think you are having 10 buttons and you assign separate click listener for each buttons. And now compare it with the above 1st option, you will realize it.

所以,我建议你去与第一种选择上面我已经建议用例子,最主要的原因是它降低code线人数和code的更好的可读性。

So i would suggest you to go with 1st option i have suggested with example above, main reason is it decrease number of code lines and better readability of code.

为什么在第一种选择更好的可读性上面我已经建议?

由于你知道,你只需要检查这个特定的功能只适用于code为每个按钮,因为一切是这里的函数内。

Because you know you just have to check this particular function only for the code for every buttons, because everything is here inside a function.

这篇关于有效性,Android的开关或点击监听器为每个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆