setOnClickListener(new OnClickListener(){}) [英] setOnClickListener(new OnClickListener(){})

查看:93
本文介绍了setOnClickListener(new OnClickListener(){})的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package android.example;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Android_eg1 extends Activity {
    Button bt; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bt=(Button)findViewById(R.id.click);
        bt.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
            // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "You made a mess", Toast.LENGTH_LONG).show();
            }

        });
      } //onCreate()

}//class

我是Java的新手,我了解Java的基础知识.这是我尝试通过按钮类了解事件处理的示例程序.我听不懂这部分:

I am new to java and I know basics of core java. This is the sample program i tried to know about the event handling through a button class. I could not understand this part :

"bt.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
        // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "You made a mess", Toast.LENGTH_LONG).show();
        }

    });"

为什么在setOnClickListener(parameter)内给出函数定义?(public void onClick(View v)})

Inside the setOnClickListener(parameter) why are they giving a function definition?(public void onClick(View v) })

这可以接受吗?

推荐答案

在Java中,这是一个实现接口的Generated Class.您可以在function参数内部动态生成一个类,也可以创建一个外部类并传递此类的实例,或者通过您的活动性并将您的活动作为侦听器传递来实现侦听器所需的接口.

In java, this is a Generated Class implementing the interface. You can generate a class on the fly inside the function argument or you can create an external class and passing a instance of this class or implements the Interface needed by your listener by your activty and passing your activity as listener.

这篇关于setOnClickListener(new OnClickListener(){})的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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