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

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

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


当前回答

不,通过代码是不可能的。当你定义Android:onClick="someMethod"属性时,Android只是实现了OnClickListener。

这两个代码段是相等的,只是以两种不同的方式实现。

代码实现

Button btn = (Button) findViewById(R.id.mybutton);

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

// some more code

public void myFancyMethod(View v) {
    // does something very interesting
}

上面是OnClickListener的代码实现。这是XML实现。

XML实现

<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:onClick="myFancyMethod" />
<!-- even more layout elements -->

在后台,Android只做Java代码,在点击事件时调用你的方法。

注意,使用上面的XML, Android将只在当前Activity中寻找onClick方法myFancyMethod()。如果你正在使用片段,记住这一点很重要,因为即使你使用片段添加上面的XML, Android也不会在用于添加XML的片段的.java文件中查找onClick方法。

我注意到另一件重要的事情。你提到你不喜欢匿名方法。你的意思是说你不喜欢匿名课程。

其他回答

为了让你的生活更简单,并避免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;
  }
}

另一种设置on click侦听器的方法是使用XML。只需添加android:onClick属性到你的标签。

尽可能在匿名Java类上使用xml属性“onClick”是一个很好的实践。

首先,让我们看看代码中的区别:

XML属性/ onClick属性

XML部分

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button1" 
    android:onClick="showToast"/>

Java部分

public void showToast(View v) {
    //Add some logic
}

匿名Java类/ setOnClickListener

XML部分

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Java部分

findViewById(R.id.button1).setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Add some logic
        }
});

下面是使用XML属性而不是匿名Java类的好处:

With Anonymous Java class we always have to specify an id for our elements, but with XML attribute id can be omitted. With Anonymous Java class we have to actively search for the element inside of the view (findViewById portion), but with the XML attribute Android does it for us. Anonymous Java class requires at least 5 lines of code, as we can see, but with the XML attribute 3 lines of code is sufficient. With Anonymous Java class we have to name of our method “onClick", but with the XML attribute we can add any name we want, which will dramatically help with the readability of our code. Xml “onClick” attribute has been added by Google during the API level 4 release, which means that it is a bit more modern syntax and modern syntax is almost always better.

当然,并不总是可以使用Xml属性,以下是我们不选择它的原因:

如果我们使用片段。onClick属性只能添加 一个活动,所以如果我们有一个片段,我们将不得不使用 匿名类。 如果我们想移动onClick侦听器到一个单独的类 (也许如果它非常复杂和/或我们想重新使用它 的不同部分),那么我们就不想使用 XML属性。

假设,你想添加这样的点击事件main.xml

<Button
    android:id="@+id/btn_register"
    android:layout_margin="1dp"
    android:layout_marginLeft="3dp"
    android:layout_marginTop="10dp"
    android:layout_weight="2"
    android:onClick="register"
    android:text="Register"
    android:textColor="#000000"/>

在java文件中,你必须写一个像这样的方法。

public void register(View view) {
}

支持Ruivo的答案,是的,你必须声明方法为“公共”,才能在Android的XML onclick中使用-我正在开发一个应用程序,从API级别8 (minSdk…)到16 (targetSdk…)

我宣称我的方法是私人的,它造成了错误,只是宣称它是公共工程。

指定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.