通过 XML 和 Java 代码使用 OnClickListener 接口有何不同? [英] How is using OnClickListener interface different via XML and Java code?

查看:23
本文介绍了通过 XML 和 Java 代码使用 OnClickListener 接口有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
OnClick() 事件和 OnClickListener 之间的区别?

我是 Android 开发的新手,当我第一次开始时,我试图通过任何必要的方式避免使用 xml 布局,因此我早期的一些项目涉及显式创建 OnClickListener 并将其实现为匿名内部类的按钮.比如-

I'm semi-new to Android development and when I first started I tried to avoid using the xml layout by any means necessary so some of my earlier projects involve buttons that explicitly create an OnClickListener and implement it as an anonymous inner class. Such as -

final Button button = new Button(this);
button.setText("Click to change second line of text");

OnClickListener buttonListener = new View.OnClickListener() {
    boolean clicked = false;
    int numClicks = 0;

    @Override
    public void onClick(View v) {
        if(numClicks > 5) {
            button.setText("STOP IT");
        }
        numClicks++;
        if(clicked == false){
            clicked = true;
            tv2.setText("Text Changed on Button Click");    
        }
        else
        {
            clicked = false;
            tv2.setText("Click again");
        }       
    }
};
button.setOnClickListener(buttonListener);

但是随着我对android越来越熟悉,我开始理解xml布局和实现这样的按钮的价值

But as I got more familiar with android, I began to understand the value of the xml layouts and implemented buttons like this

    <Button
    android:id="@+id/button1"
    android:layout_height = "wrap_content"
    android:layout_width ="wrap_content"
    android:text = "lets do this"
    android:onClick = "DoIt"
    />

在layout xml中,DoIt是在java中定义的.

In the layout xml, where DoIt was defined in the java.

我的问题是,这两种方法在功能上是一样的吗?编译器是否在幕后某处定义了 OnClickListener?您是否会通过使用一种或另一种方式来权衡任何功能?

My question is, are these 2 methods functionally the same thing? Is there an OnClickListener being defined by the compiler somewhere behind the scenes? Are there any features you trade off by using one way or the other?

推荐答案

这些完全一样.android:onClick 是在 API 级别 4 中添加的,使其更容易、更像 Javascript 网络,并从 XML 驱动一切.它在内部所做的是在按钮上添加一个 OnClickListener,它会调用您的 DoIt 方法.

These are exactly the same. android:onClick was added in API level 4 to make it easier, more Javascript-web-like, and drive everything from the XML. What it does internally is add an OnClickListener on the Button, which calls your DoIt method.

以下是使用 android:onClick="DoIt" 在内部执行的操作:

Here is what using a android:onClick="DoIt" does internally:

Button button= (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DoIt(v);
    }
});

使用 android:onClick 的唯一折衷是,像往常一样使用 XML 配置,添加动态内容变得有点困难(以编程方式,您可以决定添加一个侦听器或另一个取决于您的变量).但这很容易通过在 DoIt 方法中添加测试来解决.

The only thing you trade off by using android:onClick, as usual with XML configuration, is that it becomes a bit more difficult to add dynamic content (programatically, you could decide to add one listener or another depending on your variables). But this is easily defeated by adding your test within the DoIt method.

这篇关于通过 XML 和 Java 代码使用 OnClickListener 接口有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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