美文网首页毕业设计
actvity.findViewById() 与 view.fi

actvity.findViewById() 与 view.fi

作者: 生儿育女一辈子 | 来源:发表于2016-06-06 15:10 被阅读41次

actvity.findViewById() 与 view.findViewById()

发表于2年前(2014-12-01 09:48)   阅读(928) | 评论(11人收藏此文章,我要收藏

自从学习android的hello world开始

我们就知道了这样一个函数findViewById(),他已经成为了家喻户晓,坑蒙拐骗,杀人越货必备的一个函数(好吧,这句是扯淡)

但一直用也没细致研究过它,直到写程序的时候发现一个由这个函数引起的一个莫名其妙的bug,遂决定好好研究下次函数~

我们调用的findViewById()函数其实有两种(目前我只看到两种,不确定还有没有其他的),一种是Activity类中findViewById()函数

另外一种是View类中定义的findViewById()函数

一般我们在oncreate()方法中使用的(**view)findViewById(R.id.**)既是调用的Activity中的findViewById()函数

而在其他情况写出的***view.findViewById()中调用的是view类中的findViewById()

分别看一下源代码中的实现方法及介绍

Activity类中定义的findViewById()

[java]

/**

* Finds a view that was identified by the id attribute from the XML that

* wasprocessed in{@link#onCreate}.

*

*@returnThe view if found or null otherwise.

*/

public View findViewById(int id) {

return getWindow().findViewById(id);

}

/**

* Retrieve the current{@linkandroid.view.Window} for the activity.

* This can be used to directly access parts of the Window API that

* are not available through Activity/Screen.

*

* @return Window The current window, or null if the activity is not

*         visual.

*/

public Window getWindow() {

return mWindow;

}

从这里可以看出这个函数是在寻找在xml中定义的指定id的对象

View类中的findViewById()

[java]

/**

* Look for a child view with the given id.If this view has the given

* id, return this view.

*

* @param id The id to search for.

* @return The view that has the given id in the hierarchy or null

*/

public finalViewfindViewById(int id) {

if (id < 0) {

return null;

}

return findViewTraversal(id);

/**

*{@hide}

* @param id the id of the view to be found

* @return the view of the specified id, null if cannot be found

*/

protected View findViewTraversal(int id) {

if (id == mID) {

return this;

}

return null;

}

从这里可以看出我们是从一个view的child view中寻找指定id的对象,所以即使几个layout的XML文件中的View的id号相同的话,只要他们没有相同的父节点,或有相同的父亲节点,但不在父节点及以上节点调用findViewById通过id来查找他们就是没有问题。(这句引用自这里http://www.2cto.com/kf/201204/127404.html

使用这个函数的常见问题:

1.既然Activity中的findViewById()是从R.java中寻找东西,那么我们就要杜绝相同名字的控件

今天自己的bug就是因为这个产生的

说到控件的命名,今天还有一个小发现

仔细看下边两段代码代码

[xml]

< ?xml version="1.0" encoding="utf-8"?>

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/LinearLayout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

< /LinearLayout>

[xml]

< ?xml version="1.0" encoding="utf-8"?>

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

< /LinearLayout>

一段里边Layout没有id这个参数,一段里边有id,虽然代码不同但在outline中显示出来都是

这样在第一种情况下R.id中可以找到LinearLayout这个控件,第二种是没有的哈,这些也是以后要注意的细节

2.在调用view中的findViewById()一定要想好父View是谁!即**view.findViewById()中的**view要找对,如果没有找对父View,返回基本都是null了

相关文章

  • actvity.findViewById() 与 view.fi

    actvity.findViewById() 与 view.findViewById() 发表于2年前(2014-...

  • && 与& ,||与|

    回忆知识点i++,,++i变量在前 先用变量符号(☞++/--)在前 先计算

  • 认真与身板

    认真与身板 认真与态度 认真与自信 认真与信心 认真与诚心 认真与正心 认真与正念 认真与正面 认真与精诚 认真与...

  • 与荒野,与你,与自己

    周末了,想跟大家分享一首诗 《阿莱夫》 诗作者:赖尔逊 阿莱夫在草原上盖了一栋房子, 犹如大海上的灯塔。 但你无法...

  • 与雪与丘与故土

  • 与海与浪与念

    木君 下午,在一段段风雨的催促下来到了绥中。天是被蒙起来的,太阳早已不知躲到哪里去了。微弱的日光和着轻柔的海风洒在...

  • 晚风与柳 孤独与狗 桥与落叶 马与白隙 云与苍天 梭与星月 天与地 生与死 树与来路 花与过往 我与你 爱与恨 夜色与酒

  • 海街日记

    和解。与他人和解、与家人和解、与自己和解;与得到和解、与失去和解;与过去和解、与现在、未来和解;与现实和解、与虚幻...

  • 生怕忘了的题目

    少与不少 多与不多 苦与不苦 乐与不乐 对与不对 错与不错 离与不离 合与不合 唱与不唱 说与不说

  • 2017-04-11

    体验入:真诚.与专业。幽默与风趣。赞美与了解。认可与相信。沟通与关注。关心与引领。快乐与持续。简单与重复。 ...

网友评论

    本文标题:actvity.findViewById() 与 view.fi

    本文链接:https://www.haomeiwen.com/subject/ectddttx.html