Java Android setOnclicklistener 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9699844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:21:45  来源:igfitidea点击:

Android setOnclicklistener parameter

javaandroid

提问by Ahmed Waheed

I'm a beginner to android, while setting onclick listener to a button, what does the parameter passed mean:

我是android的初学者,在将onclick监听器设置为按钮时,传递的参数是什么意思:

 btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });    

so, "new OnClickListener()" it is passed to a method then shouldn't that be an object?
if it is not an object; then how do we use "new" keyword?
why "onclick()" is called directly without using the "this" keyword, or its class name.

所以,“new OnClickListener()”它被传递给一个方法,那么它不应该是一个对象吗?
如果它不是一个对象;那么我们如何使用“new”关键字呢?
为什么不使用“this”关键字或其类名直接调用“onclick()”。

回答by Blackbelt

OnClickListener()

OnClickListener()

is an interface. With the new keyword you are declaring a new object of an anonymous inner class that will implements the interface. That s base question for

是一个接口。使用 new 关键字声明将实现接口的匿名内部类的新对象。这是基本问题

java

爪哇

than android. You should consider reading about

比安卓。你应该考虑阅读关于

java interface

接口

回答by waqaslam

OnClickListeneris an interfaceand by using new OnClickListener()as parameter for btn1.setOnClickListenerit is actually creating an Anonymous Inner Classwhich implements OnClickListener. And the method onClickmust need to be declared since its an abstract method inside that interface class. Any code you write inside onClick will be executed when the button is pressed.

OnClickListener是一个接口,通过new OnClickListener()用作btn1.setOnClickListener 的参数,它实际上创建了一个实现OnClickListener匿名内部类。并且方法onClick必须需要声明,因为它是该接口类中的抽象方法。当按钮被按下时,你在 onClick 中编写的任何代码都会被执行。



Update

更新

to do it using Anonymous inner class in an object:

在对象中使用匿名内部类来做到这一点:

//declaring OnClickListener as an object
private OnClickListener btnClick = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
};

//passing listener object to button
btn1.setOnClickListener(btnClick);

to do it without using Anonymous class:

在不使用匿名类的情况下做到这一点:

public class YourActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       Button b = new Button(this);

       //setting listener to button
       b.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}

The only difference between these approaches is, if your button contains click event code which is supposed to be valid/available for that button only, then you may use inner class as you are doing in your code (because its easy to do right away). However if there are multiple buttons which require same code to be executed on onClick event then you may define listener as an object and pass it to them.

这些方法之间的唯一区别是,如果您的按钮包含应该仅对该按钮有效/可用的单击事件代码,那么您可以像在代码中一样使用内部类(因为它很容易立即执行) . 但是,如果有多个按钮需要在 onClick 事件上执行相同的代码,那么您可以将侦听器定义为一个对象并将其传递给它们。

回答by Dheeraj Vepakomma

Read up on anonymous classesin Java.

阅读Java 中的匿名类

回答by Chris.Jenkins

OnClickListeneris an interface. It means if you set it inline you have created an anonymous class which is just implimenting the interface inside the set method.

OnClickListener是一个接口。这意味着如果您将其设置为内联,则您已经创建了一个匿名类,该类只是在 set 方法中实现了接口。

If you wanted to create an OnClickclass. You would do something like this:

如果你想创建一个OnClick类。你会做这样的事情:

class OnButtonClick implements OnClickListener{

    public void onClick(View v) {
        // TODO Auto-generated method stub
    }

}

Then you can use:

然后你可以使用:

OnButtonClick obc = new OnButtonClick();
textView.setOnClickListener(obc);

This is more Java than android, Read about Interfaces and Inner Classes

这比 android 更 Java,阅读接口和内部类

回答by Chris.Jenkins

Interface definition for a callback to be invoked when a view is clicked.

单击视图时要调用的回调的接口定义。

abstract void onClick(View v) Called when a view has been clicked.

abstract void onClick(View v) 在单击视图时调用。

回答by Eng.Fouad

To instantiate an object of an interface or an abstract class, you need to override all of its not implemented methods (abstract methods). You can override the abstract methods by either using anonymous class or defining a class that extends the abstract class/implements the interface and override the abstract methods. However, to make it clearer, you can do it like this:

要实例化接口或抽象类的对象,您需要覆盖其所有未实现的方法(抽象方法)。您可以通过使用匿名类或定义扩展抽象类/实现接口并覆盖抽象方法的类来覆盖抽象方法。但是,为了更清楚,您可以这样做:

OnClickListener l = new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
    }
});
btn1.setOnClickListener(l);

and to use a separate class, you can do it like this:

并使用一个单独的类,你可以这样做:

btn1.setOnClickListener(new MyOwnListener());

// ...

public class MyOwnListener implements OnClickListener
{
    // ...

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
    }
}