关于findViewById函数
来源:互联网 发布:qq飞车ae86原装数据 编辑:程序博客网 时间:2024/06/05 11:02
我们调用的findViewById()函数其实有两种(目前我只看到两种,不确定还有没有其他的),一种是Activity类中findViewById()函数;另外一种是View类中定义的findViewById()函数。
[java]
/**
* Finds a view that was identified by the id attribute from the XML that
* was processed in {@link #onCreate}.
*
* @return The view if found or null otherwise.
*/
public View findViewById(int id) {
return getWindow().findViewById(id);
}
/**
* Retrieve the current {@link android.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的对象
/**
* 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 final View findViewById(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来查找他们就是没有问题。
在调用view中的findViewById()一定要想好父View是谁!即**view.findViewById()中的**view要找对,如果没有找对父View,返回基本都是null了。
一般我们在oncreate()方法中使用的(**view)findViewById(R.id.**)既是调用的Activity中的findViewById()函数而在其他情况写出的***view.findViewById()中调用的是view类中的findViewById()。
分别看一下源代码中的实现方法及介绍
1、Activity类中定义的findViewById()[java]
/**
* Finds a view that was identified by the id attribute from the XML that
* was processed in {@link #onCreate}.
*
* @return The view if found or null otherwise.
*/
public View findViewById(int id) {
return getWindow().findViewById(id);
}
/**
* Retrieve the current {@link android.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的对象
2、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 final View findViewById(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来查找他们就是没有问题。
在调用view中的findViewById()一定要想好父View是谁!即**view.findViewById()中的**view要找对,如果没有找对父View,返回基本都是null了。
- 关于findViewById函数
- 初学Android—关于findViewById函数
- 关于findViewById
- 关于findviewbyid
- 关于findViewByid的简写
- android findviewByid()关于空指针
- 获取控件ID函数findViewById
- FindViewById
- findViewById
- findViewById
- findViewById
- findViewById
- findViewById
- 关于android findViewById 返回为空问题
- 关于findViewById你所不知道的
- inflate()和findViewById()函数的用法
- android新手上路(二)--- 关于findViewById 返回为空
- 关于在adapter使用LinearLayout的findviewbyid问题
- LeetCode:Symmetric Tree
- 价值投资的误区
- hdu1033(水题)
- Magento How To Display Product Custom Option On list.phtml
- visual studio 2012 创建向导时,处理模板文件时出错的解决方法
- 关于findViewById函数
- hdu1031
- Poj 2299 Ultra-QuickSort(归并排序求逆序数)
- 矩阵经典操作(4)
- 搬家啦,CSDN我来了,博客园再见啦
- 【整数操作】02.递归公式的非递归做法
- 老码农教你学英语
- 产品经理必读的10篇文章
- Win7下要求以管理员权限运行程序