我可以指定一个"默认" OnClickListener()的一个Android的活动? [英] Can I assign a "default" OnClickListener() for an Android Activity?

查看:127
本文介绍了我可以指定一个"默认" OnClickListener()的一个Android的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,在布局每个插件,我叫setOnClickListener的活动()来分配我的OnClick()处理程序。在我的OnClick()处理我用switch语句来执行所需code的基础上,查看参数的ID每个按钮。有没有办法来分配,而不必在查看每个插件使个人监听分配调用的默认处理程序的主要观点?

I have an Activity that, for each widget in the layout, I call setOnClickListener() to assign my OnClick() handler. In my OnClick() handler I use a switch statement to execute the desired code for each button based on the View parameter's ID. Is there a way to assign a default handler to the main view instead of having to make individual listener assignment calls for each widget in the view?

=============================================== =

================================================

更新

由于kcoppock的起始样品我有codeD了一个完整的实现,具有设置单击处理程序中的活动,以一个共同的点击处理程序的所有视图元素的静态方法的类。这对于那些你有一个简单的布局和你想要做的所有的事件处理在使用基于视图参数对象的ID switch语句中一个常见的​​点击侦听器事件的情况。要从活动使用它,只需调用 Misc.setDefaultClickHandler(这一点,这一点)。当然你的Activity需要实现View.OnclickListener接口。

Thanks to kcoppock's starting sample I have coded up a complete implementation of a class that has a static method that sets the click handler for all View elements in an Activity to a common click handler. This is for those situations where you have a simple layout and you want to do all the event handling in a common click listener event that uses a switch statement based on the View parameter object's ID. To use it from an Activity, just call Misc.setDefaultClickHandler(this, this). Naturally your Activity needs to implement the View.OnclickListener interface.

package {put your package name here};

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;

public class Misc {

    public Misc() {
        super();
    }

    // Returns the root view for a given activity.
    public static View getRootView(Activity activity) {
        return activity.findViewById(android.R.id.content).getRootView();
    }

    private static void assignClickHandler(View root, View.OnClickListener theOnClickListener) {

        // Is it a View or a View group?
        if (root instanceof ViewGroup) {

            // It's a ViewGroup, process all it's children.
            ViewGroup vg = (ViewGroup) root;

            for(int i = 0; i < vg.getChildCount(); i++) 
                // Make recursive call.
                assignClickHandler(vg.getChildAt(i), theOnClickListener);
        } 
        else 
        {
            // Child is a View.  Set the desired context for the click handler.
            root.setOnClickListener(theOnClickListener);
        }
    }

    public static void setDefaultClickHandler(Activity activity, View.OnClickListener theOnClickListener) {
        assignClickHandler(getRootView(activity), theOnClickListener);
    }
}

- roschler

-- roschler

推荐答案

据我所知,但你可以只使用一个循环,是这样的:

Not to my knowledge, but you could just use a loop, something like this:

ViewGroup root = findViewById(R.id.my_root_layout);
final Context context = this;
assignClickHandler(root);

public void assignClickHandler(int root) {
    for(int i = 0; i < root.getChildCount(); i++) {
        if(root.getChildAt(i) instanceof ViewGroup) {
            assignClickHandler(root.getChildAt(i));
        }
        else {
            (root.getChildAt(i)).setOnClickListener(context);
        }
    }
}

请注意,它调用递归任何嵌套布局内为好。我没有测试过这个,所以我可能会搞砸了一些语法,但这个想法应该工作,如果你只是希望避免手动设定每次一片。

Note it calls recursively for any nested layouts within as well. I haven't tested this so I might have messed up some syntax, but that idea should work, if you're just looking to avoid manually setting every one.

这篇关于我可以指定一个&QUOT;默认&QUOT; OnClickListener()的一个Android的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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