我可以指定一个“默认”吗? Android Activity 的 OnClickListener()?
我有一个 Activity,对于布局中的每个小部件,我调用 setOnClickListener() 来分配我的 OnClick() 处理程序。在我的 OnClick() 处理程序中,我使用 switch 语句根据 View 参数的 ID 为每个按钮执行所需的代码。有没有办法将默认处理程序分配给主视图,而不必为视图中的每个小部件进行单独的侦听器分配调用?
=================================================
<强>更新
感谢 kcoppock 的起始示例,我编写了一个类的完整实现,该类具有一个静态方法,该方法将 Activity 中所有视图元素的单击处理程序设置为公共单击处理程序。这适用于这样的情况:您有一个简单的布局,并且希望在公共单击侦听器事件中执行所有事件处理,该事件使用基于 View 参数对象 ID 的 switch 语句。要从 Activity 中使用它,只需调用 Misc.setDefaultClickHandler(this, this)。当然,您的 Activity 需要实现 View.OnclickListener 接口。
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);
}
}
——罗施勒
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?
================================================
UPDATE
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,但您可以只使用循环,如下所示:
请注意,它也会递归调用其中的任何嵌套布局。我还没有测试过这个,所以我可能搞乱了一些语法,但如果你只是想避免手动设置每一个,那么这个想法应该可行。
Not to my knowledge, but you could just use a loop, something like this:
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.