XML中的Android onClick与OnClickListener [英] Android onClick in XML vs. OnClickListener

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

问题描述

我意识到之前有人问过类似措词的问题,但这是不同的.我在开发android应用程序时还很陌生,关于android:onclick="" XML属性和setOnClickListener方法之间的区别,我有三个问题.

I realize that a similarly-worded question has been asked before, but this is different. I am pretty new at developing android apps and I have three questions regarding the difference(s) between the android:onclick="" XML attribute and the setOnClickListener method.

  1. 两者之间有什么区别?两种实现之间的区别是在编译时还是在运行时或在两者时发现的?

  1. What are the differences between the two? Is the difference between the two implementations found at compile time or run time or both?

哪些用例适合哪种实现?

What use cases are favorable to which implementation?

Android中使用片段对实现选择有何不同?

What difference(s) does the use of fragments in Android make in implementation choice?

推荐答案

OnClickListener与OnClick之间的区别:

  • OnClickListener是您需要实现的界面,可以对其进行设置 到Java代码中的视图.
  • OnClickListener是等待某人的东西 要真正点击,onclick会确定当某人发生时会发生什么 点击.
  • 最近android向名为android:onclick的视图中添加了xml属性, 可用于直接在视图活动中处理点击 无需实现任何接口.
  • 如果需要,您可以轻松地将一个侦听器实现与另一个侦听器实现交换.
  • 通过OnClickListener,您可以将click事件的操作/行为与触发事件的View分开.尽管对于简单的情况而言,这并不是什么大问题,但是对于复杂的事件处理,这可能意味着更好的代码可读性和可维护性
  • 由于OnClickListener是一个接口,因此实现它的类可以灵活地确定处理事件所需的实例变量和方法.同样,在简单情况下这不是什么大问题,但是对于复杂情况,我们不想将与事件处理相关的变量/方法与触发事件的View代码混在一起.
  • XML布局中具有函数绑定的onClick是onClick和它将调用的函数之间的绑定.该函数必须具有一个参数(视图")才能使onClick起作用.
  • OnClickListener is the interface you need to implement and can be set to a view in java code.
  • OnClickListener is what waits for someone to actually click, onclick determines what happens when someone clicks.
  • Lately android added a xml attribute to views called android:onclick, that can be used to handle clicks directly in the view's activity without need to implement any interface.
  • You could easily swap one listener implementation with another if you need to.
  • An OnClickListener enable you to separate the action/behavior of the click event from the View that triggers the event. While for simple cases this is not such a big deal, for complex event handling, this could mean better readability and maintainability of the code
  • Since OnClickListener is an interface, the class that implements it has flexibilities in determining the instance variables and methods that it needs in order to handle the event. Again, this is not a big deal in simple cases, but for complex cases, we don't want to necessary mix up the variables/methods that related to event handling with the code of the View that triggers the event.
  • The onClick with function binding in XML Layout is a binding between onClick and the function that it will call. The function have to have one argument (the View) in order for onClick to function.

两个函数的用法相同,只是一个通过java代码设置,另一个通过xml代码设置.

Both function the same way, just that one gets set through java code and the other through xml code.

setOnClickListener代码实现:

Button btn = (Button) findViewById(R.id.mybutton);

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    myFancyMethod(v);
    }
});

// some more code

public void myFancyMethod(View v) {
    // does something very interesting
}

XML实现:

<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:onClick="myFancyMethod" />
<!-- even more layout elements -->

性能:

两者的性能相同.编译时将Xml预解析为二进制代码.因此Xml中没有开销.

Both are the same in performance. Xml is pre-parsed into binary code while compiling. so there is no over-head in Xml.

限制条件:

android:onClick适用于API级别4以上,因此,如果您定位< 1.6,那么您将无法使用它.

android:onClick is for API level 4 onwards, so if you're targeting < 1.6, then you can't use it.

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

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