android:onClick XML 属性与 setOnClickListener 有何不同? [英] How exactly does the android:onClick XML attribute differ from setOnClickListener?

查看:25
本文介绍了android:onClick XML 属性与 setOnClickListener 有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,您可以通过两种方式将 onClick 处理程序分配给按钮.

From that I've read you can assign a onClick handler to a button in two ways.

使用 android:onClick XML 属性,您只需使用带有签名的公共方法的名称void name(View v) 或使用 setOnClickListener 方法,您可以在其中传递实现 OnClickListener 接口的对象.后者通常需要一个我个人不喜欢的匿名类(个人品味)或定义一个实现 OnClickListener 的内部类.

Using the android:onClick XML attribute where you just use the name of a public method with the signaturevoid name(View v) or by using the setOnClickListener method where you pass an object that implement the OnClickListener interface. The latter often requires an anonymous class which personally I don't like (personal taste) or defining an internal class that implements the OnClickListener.

通过使用 XML 属性,你只需要定义一个方法而不是一个类,所以我是想知道是否可以通过代码而不是在 XML 布局中完成相同的操作.

By using the XML attribute you just need to define a method instead of a class so I was wondering if the same can be done via code and not in the XML layout.

推荐答案

不,这不能通过代码实现.当您定义 android:onClick="someMethod" 属性时,Android 只会为您实现 OnClickListener.

No, that is not possible via code. Android just implements the OnClickListener for you when you define the android:onClick="someMethod" attribute.

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

Those two code snippets are equal, just implemented in two different ways.

代码实现

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 实现.

Above is a code implementation of an OnClickListener. And this is the XML implementation.

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 代码,在点击事件上调用您的方法.

In the background, Android does nothing else than the Java code, calling your method on a click event.

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

Note that with the XML above, Android will look for the onClick method myFancyMethod() only in the current Activity. This is important to remember if you are using fragments, since even if you add the XML above using a fragment, Android will not look for the onClick method in the .java file of the fragment used to add the XML.

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

Another important thing I noticed. You mentioned you don't prefer anonymous methods. You meant to say you don't like anonymous classes.

这篇关于android:onClick XML 属性与 setOnClickListener 有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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