在Android的按钮单击监听器监听器、单击、按钮、Android

由网友(碎泪)分享简介:可能重复: Difference的OnClick()事件和OnClickListener? 我是半新的Andr​​oid开发,当我第一次开始我试图避免使用XML布局任何所以我的一些早期的项目涉及明确创建一个OnClickListener并执行它作为一个匿名内部类的按钮必要手段。比如 - I'm semi-new...

可能重复:   Difference的OnClick()事件和OnClickListener?

我是半新的Andr​​oid开发,当我第一次开始我试图避免使用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"
    />

在布局XML,其中DOIT是在Java中定义的。

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

我的问题是,这2种方法在功能上是一回事吗?有没有编译器的地方在幕后所定义的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?

推荐答案

这些是完全一样的。 安卓的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);
    }
});

您使用折中的唯一的事情安卓的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.

阅读全文

相关推荐

最新文章