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

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

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


当前回答

另一种设置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属性。

其他回答

小心,虽然android:onClick XML似乎是一个方便的方式来处理点击,setOnClickListener实现做一些额外的添加onClickListener。实际上,它把视图属性clickable设为true。

虽然这可能不是大多数Android实现的问题,根据手机构造函数,按钮总是默认为clickable = true,但在一些手机模型上的其他构造函数可能在非button视图上有默认clickable = false。

所以设置XML是不够的,你必须一直考虑在非按钮上添加android:clickable="true",如果你有一个默认为clickable= true的设备,你甚至忘记放置这个XML属性,你不会在运行时注意到这个问题,但会在市场上得到反馈,当它将在你的客户手中!

此外,我们永远无法确定proguard将如何混淆和重命名XML属性和类方法,所以不是100%安全,它们永远不会有任何错误。

因此,如果你不想有麻烦,也不想想它,最好使用setOnClickListener或ButterKnife这样的库,annotation @OnClick(R.id.button)

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

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

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

另一种设置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属性。

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

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

检查一下是否忘记将方法设置为public!