安卓的onclick在XML VS OnClickListener [英] Android onclick in xml vs OnClickListener

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

问题描述

我认识到,一个措词类似的问题已经被问过,但这次不同。我是pretty的新的开发Android应用程序和我有一个关于在Android之间的差额(S)三个问题:的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)什么是两者之间的区别是什么?在编译时或运行时或同时发现两者implimentations之间的区别是什么?

1) What are the differences between the two? Is the difference between the two implimentations found at compile time or runtime or both?

2)什么用例有利于它执行力度?

2) What use cases are favorable to which implimentation?

3)有什么区别(s)没有使用android的化妆片段的执行力度的选择吗?

3) What difference(s) does the use of fragments in android make in implimentation choice?

推荐答案

之间OnClickListener差Vs的OnClick:

  • OnClickListener是需要实现,并且可以设置界面 在Java code向视图。
  • OnClickListener是等待别人 实际点击,的onclick确定会发生什么,当有人 点击。
  • 在最近的Andr​​oid添加了一个XML属性的观点叫做Android:的onclick, 可用于直接在视图的活动处理的点击 而不需要实现任何接口。
  • 您可以方便地更换一个监听器实现与另一个,如果你需要。
  • 在一个OnClickListener使您能够从触发事件的景观独立的单击事件的动作/行为。虽然简单的情况下,这不是什么大不了的,对复杂事件处理,这可能意味着code
  • 更好的可读性和可维护性
  • 由于OnClickListener是一个接口,实现它在确定该实例变量和方法,它需要以处理该事件的灵活性的类。再次,这是不是一个大问题,在简单的情况下,但对于复杂的情况下,我们不希望在必要混淆的变量/处理与触发事件的视图的code,有关事件的方法。
  • 与功能的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 code组和其他通过xml code。

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

setOnClickListener code实现的:

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是pre-解析成二进制code,而编制。所以没有过度头XML。

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

限制:

安卓的onClick是API级别4起,所以如果你的目标&LT; 1.6,那么你就不能使用它。

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

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

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