Android-setOnClickListener与OnClickListener与View.OnClickListener [英] Android - setOnClickListener vs OnClickListener vs View.OnClickListener

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

问题描述

我的理解是,当我创建一个监听点击的按钮对象时,我必须:

My understanding is that when I'm creating a button object that listens for a click, I have to:

  1. 创建按钮对象
  2. 使用OnClickListner使其监听用户的点击
  3. 在用户单击按钮后使用onClick执行操作
  1. Create the button object
  2. Use OnClickListner to make it listen to the user's click
  3. Use onClick to execute actions after the user clicks the button

现在,

  • setOnClickListener在哪里适合上述逻辑?
  • 实际上是哪个人在听按钮点击?
  • setOnClickListener?
  • OnClickListener?
  • View.OnClickListener?
  • 这三个之间有什么区别?
  • Where does setOnClickListener fit into the above logic?
  • Which one actually listens to the button click?
  • setOnClickListener?
  • OnClickListener?
  • View.OnClickListener?
  • What are the differences between those three?

这是我在Android Dev中找到的内容:

Here is what I found in Android Dev:

//The example below shows how to register an on-click listener for a Button.

// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

protected void onCreate(Bundle savedValues) {
    ...
    // Capture our button from layout
    Button button = (Button)findViewById(R.id.corky);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(mCorkyListener);
    ...
}

您可能还会发现,将OnClickListener作为您的Activity一部分的实现更为方便.这将避免额外的类负载和对象分配.例如:

You may also find it more convenient to implement OnClickListener as a part of your Activity. This will avoid the extra class load and object allocations. For example:

public class ExampleActivity extends Activity implements OnClickListener {
    protected void onCreate(Bundle savedValues) {
        ...
         Button button = (Button)findViewById(R.id.corky);
         button.setOnClickListener(this);
    }

    // Implement the OnClickListener callback
    public void onClick(View v) {
      // do something when the button is clicked
    }
}

推荐答案

逻辑很简单. setOnClickListener属于步骤2.

The logic is simple. setOnClickListener belongs to step 2.

  1. 您创建按钮
  2. 您可以像在该示例中一样创建OnClickListener *的实例,并覆盖onClick-方法.
  3. 您可以在片段/活动onCreate-方法中使用btn.setOnClickListener(myOnClickListener);将该OnClickListener分配给该按钮.
  4. 当用户单击按钮时,将调用分配的OnClickListeneronClick功能.
  1. You create the button
  2. You create an instance of OnClickListener* like it's done in that example and override the onClick-method.
  3. You assign that OnClickListener to that button using btn.setOnClickListener(myOnClickListener); in your fragments/activities onCreate-method.
  4. When the user clicks the button, the onClick function of the assigned OnClickListener is called.

*如果您使用import android.view.View;,则使用View.OnClickListener.如果您import android.view.View.*;import android.view.View.OnClickListener;,请按照我的理解使用OnClickListener.

*If you import android.view.View; you use View.OnClickListener. If you import android.view.View.*; or import android.view.View.OnClickListener; you use OnClickListener as far as I get it.

另一种方法是让您的活动/片段继承自OnClickListener.这样,您可以将片段/活动分配为按钮的侦听器,并将onClick实现为成员函数.

Another way is to let you activity/fragment inherit from OnClickListener. This way you assign your fragment/activity as the listener for your button and implement onClick as a member-function.

这篇关于Android-setOnClickListener与OnClickListener与View.OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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