在android系统实现View.OnClickListener最好的方法 [英] Best way to implement View.OnClickListener in android

查看:551
本文介绍了在android系统实现View.OnClickListener最好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有很多意见的活动上 OnClickListener 是要注册。

Suppose we have an Activity with a lot of views on which OnClickListener is to be registered.

要实现这种最常见的方式是让活动子类实现OnClickListener,是这样的:

The most common way to implement this is to let the Activity-Subclass implement the OnClickListener, something like this:

public class ActivityMain extends Activity implements View.OnClickListener
{   
    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            //handle multiple view click events
        }
    }
}

我想实现它的方法是创建活动,子类中的私有类,并让这种内部类
实现OnClickListener:

The way i like to implement it is to create a private class inside the Activity-Subclass and let that inner class implement the OnClickListener:

public class ActivityMain extends Activity implements View.OnClickListener
{
    private class ClickListener implements View.OnClickListener
    {   
        @Override
        public void onClick(View view)
        {
            switch (view.getId())
            {
                //handle multiple view click events
            }
        }
    }
}

这样,code看起来更有条理,便于维护。

This way the code seems more organized and easy to maintain.

此外,谈到是一个,有-a关系,后者似乎是一个很好的做法,因为现在
活动的子类具有有,一个与ClickListener关系。
而在以前的方法中,我们会说,我们的活动,子类是一个ClickListener,这是不完全正确的。

Moreover, talking about "Is-a", "Has-a" relationships, the latter seems to be a good practice because now the Activity-Subclass would have a "Has-a" relationship with the ClickListener. While in the former method we would be saying that Our Activity-Subclass "Is-a" ClickListener, which ain't completely true.

需要注意的是,我不关心内存开销,后者将导致。

Note that, i am not concerned with the memory overhead the latter would cause.

另外,在XML中添加的onClick标签是完全不成问题的。

Also, adding onClick tag in xml is completely out of question.

那么,什么是真正实现ClickListener的最佳方式?

So, what really is the best way to implement a ClickListener?

请不要建议像RoboGuice或奶油刀等任何库。

Please don't suggest any libraries like RoboGuice or ButterKnife etc.

推荐答案

首先有由Android就点击注册听众中没有定义的最佳实践。这完全取决于你的使用情况。

Firstly there is no best practice defined by android regarding registering click listener. It Totally depends on your use case.

实施 View.OnClickListener 界面活动是要走的路。由于Android的强烈一遍又一遍它是一个活动或片段。建议接口的实现。

Implementing the View.OnClickListener interface to activity is the way to go. As Android strongly recommends interface implementation over and over again whether its is a activity or fragment.

现在像你描述的:

public class ActivityMain extends Activity implements View.OnClickListener
{
    private class ClickListener implements View.OnClickListener
    {   
        @Override
        public void onClick(View view)
        {
            switch (view.getId())
            {
                //handle multiple view click events
            }
        }
    }
}

这是你的approach.Now它是你的执行没有错方式,如果你不与内存有关overhead.But什么创建内部类和实施View.OnClickListener如果你能简单地实现,为造福主类也可导致code清晰和简单,你所需要的。

This is your approach.Now it is your way of implementation nothing wrong with this if you are not concerned with memory overhead.But whats the benefit of creating the inner class and implementing the View.OnClickListener if you can simply implement that to main class which can also lead to code clarity and simplicity that you need.

所以它只是一个讨论,而越来越implemeting的最佳解决方案在 View.OnClickListener ,因为如果你从每个人的实际情况来看去,会去一个解决方案是简单和内存efficent。

So It just an discussion rather getting the best possible solution of implemeting the View.OnClickListener because if you go from practical point of everyone, will go for a solution which is simple and memory efficent.

所以我想preFER的传统方式,让事情变得简单,高效如下:

So i would prefer the conventional way, keep things simple and efficient as below :

@Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            //handle multiple view click events
        }
    }

P.S:您的方法将defintely增加LOC:P)

这篇关于在android系统实现View.OnClickListener最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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