从我所读到的,你可以用两种方式为按钮分配onClick处理程序。

使用android:onClick XML属性,你只是使用一个公共方法的名称与signaturevoid名称(View v)或通过使用setOnClickListener方法,你传递一个实现OnClickListener接口的对象。后者通常需要一个匿名类,我个人不喜欢(个人口味)或定义一个实现OnClickListener的内部类。

通过使用XML属性,你只需要定义一个方法而不是一个类 想知道同样的事情是否可以通过代码而不是在XML布局中完成。


当前回答

指定android:onClick属性会导致Button实例在内部调用setOnClickListener。因此两者绝对没有区别。

为了更清楚地理解,让我们看看框架是如何处理XML onClick属性的。

当布局文件被膨胀时,其中指定的所有视图都会被实例化。在这个特定的情况下,Button实例是使用公共Button (Context Context, AttributeSet attrs, int defStyle)构造函数创建的。XML标记中的所有属性都是从资源包中读取的,并作为AttributeSet传递给构造函数。

Button类继承自View类,它导致View构造函数被调用,View构造函数通过setOnClickListener负责设置点击回调处理程序。

在attrs.xml中定义的onClick属性在View.java中被引用为r.s eleable . view_onclick。

下面是View.java的代码,它通过自己调用setOnClickListener来为您完成大部分工作。

 case R.styleable.View_onClick:
            if (context.isRestricted()) {
                throw new IllegalStateException("The android:onClick attribute cannot "
                        + "be used within a restricted context");
            }

            final String handlerName = a.getString(attr);
            if (handlerName != null) {
                setOnClickListener(new OnClickListener() {
                    private Method mHandler;

                    public void onClick(View v) {
                        if (mHandler == null) {
                            try {
                                mHandler = getContext().getClass().getMethod(handlerName,
                                        View.class);
                            } catch (NoSuchMethodException e) {
                                int id = getId();
                                String idText = id == NO_ID ? "" : " with id '"
                                        + getContext().getResources().getResourceEntryName(
                                            id) + "'";
                                throw new IllegalStateException("Could not find a method " +
                                        handlerName + "(View) in the activity "
                                        + getContext().getClass() + " for onClick handler"
                                        + " on view " + View.this.getClass() + idText, e);
                            }
                        }

                        try {
                            mHandler.invoke(getContext(), View.this);
                        } catch (IllegalAccessException e) {
                            throw new IllegalStateException("Could not execute non "
                                    + "public method of the activity", e);
                        } catch (InvocationTargetException e) {
                            throw new IllegalStateException("Could not execute "
                                    + "method of the activity", e);
                        }
                    }
                });
            }
            break;

如您所见,调用setOnClickListener来注册回调,就像我们在代码中所做的那样。唯一的区别是它使用Java Reflection来调用Activity中定义的回调方法。

以下是其他回答中提到的问题的原因:

Callback method should be public : Since Java Class getMethod is used, only functions with public access specifier are searched for. Otherwise be ready to handle IllegalAccessException exception. While using Button with onClick in Fragment, the callback should be defined in Activity : getContext().getClass().getMethod() call restricts the method search to the current context, which is Activity in case of Fragment. Hence method is searched within Activity class and not Fragment class. Callback method should accept View parameter : Since Java Class getMethod searches for method which accepts View.class as parameter.

其他回答

通过使用XML属性,您只需要定义一个方法而不是 一个类,所以我想知道如果同样可以通过代码而不是在 XML布局。

是的,你可以让你的片段或活动实现视图。OnClickListener

当你在代码中初始化你的新视图对象时,你可以简单地做mView.setOnClickListener(this);

这将自动设置代码中的所有视图对象使用onClick(view v)方法,您的片段或活动等。

为了区分哪个视图调用了onClick方法,你可以在v.getId()方法上使用switch语句。

这个答案不同于“不,通过代码是不可能的”

注意,如果您想使用onClick XML特性,相应的方法应该有一个参数,其类型应该与XML对象匹配。

例如,一个按钮将通过它的名称字符串链接到你的方法:android:onClick="MyFancyMethod",但方法声明应该显示: ...MyFancyMethod(View v){…

如果您试图将此功能添加到菜单项,它将在XML文件中具有完全相同的语法,但您的方法将被声明为:…MyFancyMethod(MenuItem mi){…

为了让你的生活更简单,并避免setOnClicklistener()中的匿名类,实现一个视图。OnClicklistener接口如下:

公共类YourClass扩展了CommonActivity,实现了View。OnClickListener,……

这避免了:

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        yourMethod(v);
    }
});

并直接指向:

@Override
public void onClick(View v) {
  switch (v.getId()) {
    case R.id.your_view:
      yourMethod();
      break;
  }
}

指定android:onClick属性会导致Button实例在内部调用setOnClickListener。因此两者绝对没有区别。

为了更清楚地理解,让我们看看框架是如何处理XML onClick属性的。

当布局文件被膨胀时,其中指定的所有视图都会被实例化。在这个特定的情况下,Button实例是使用公共Button (Context Context, AttributeSet attrs, int defStyle)构造函数创建的。XML标记中的所有属性都是从资源包中读取的,并作为AttributeSet传递给构造函数。

Button类继承自View类,它导致View构造函数被调用,View构造函数通过setOnClickListener负责设置点击回调处理程序。

在attrs.xml中定义的onClick属性在View.java中被引用为r.s eleable . view_onclick。

下面是View.java的代码,它通过自己调用setOnClickListener来为您完成大部分工作。

 case R.styleable.View_onClick:
            if (context.isRestricted()) {
                throw new IllegalStateException("The android:onClick attribute cannot "
                        + "be used within a restricted context");
            }

            final String handlerName = a.getString(attr);
            if (handlerName != null) {
                setOnClickListener(new OnClickListener() {
                    private Method mHandler;

                    public void onClick(View v) {
                        if (mHandler == null) {
                            try {
                                mHandler = getContext().getClass().getMethod(handlerName,
                                        View.class);
                            } catch (NoSuchMethodException e) {
                                int id = getId();
                                String idText = id == NO_ID ? "" : " with id '"
                                        + getContext().getResources().getResourceEntryName(
                                            id) + "'";
                                throw new IllegalStateException("Could not find a method " +
                                        handlerName + "(View) in the activity "
                                        + getContext().getClass() + " for onClick handler"
                                        + " on view " + View.this.getClass() + idText, e);
                            }
                        }

                        try {
                            mHandler.invoke(getContext(), View.this);
                        } catch (IllegalAccessException e) {
                            throw new IllegalStateException("Could not execute non "
                                    + "public method of the activity", e);
                        } catch (InvocationTargetException e) {
                            throw new IllegalStateException("Could not execute "
                                    + "method of the activity", e);
                        }
                    }
                });
            }
            break;

如您所见,调用setOnClickListener来注册回调,就像我们在代码中所做的那样。唯一的区别是它使用Java Reflection来调用Activity中定义的回调方法。

以下是其他回答中提到的问题的原因:

Callback method should be public : Since Java Class getMethod is used, only functions with public access specifier are searched for. Otherwise be ready to handle IllegalAccessException exception. While using Button with onClick in Fragment, the callback should be defined in Activity : getContext().getClass().getMethod() call restricts the method search to the current context, which is Activity in case of Fragment. Hence method is searched within Activity class and not Fragment class. Callback method should accept View parameter : Since Java Class getMethod searches for method which accepts View.class as parameter.

这里有很好的答案,但我想补充一句:

在XML中的android:onclick中,android在后台使用java反射来处理这个问题。

正如这里所解释的,反射总是会降低性能。(特别是Dalvik VM)。注册clicklistener是一种更好的方法。