android.app.Activity中
publicViewfindViewById(intid)
Since:APILevel1
FindsaviewthatwasidentifiedbytheidattributefromtheXMLthatwasprocessedinonCreate(Bundle).
Returns
*Theviewiffoundornullotherwise.
publicViewfindViewById(intid)
Since:APILevel1
FindsaviewthatwasidentifiedbytheidattributefromtheXMLthatwasprocessedinonCreate(Bundle).
Returns
*Theviewiffoundornullotherwise.
这里的findViewById是在当前Active的View根及其儿子中通过Id寻找View
Active的 View 根一般是在其在 onCreate函数 中可以通过 setContentView(R.layout.main) 这种形式来设置。
android.view.View中
publicfinalView findViewById (intid)
Since:APILevel1
Lookforachildviewwiththegivenid.Ifthisviewhasthegivenid,returnthisview.
Parameters
id Theidtosearchfor.
Returns
*Theviewthathasthegivenidinthehierarchyornull
因为是 findViewById在是从View及其儿子中查找,所以即使几个layout的XML文件中的View的id号相同的话,只要他们没有相同的父节点 , 或有相同的父亲节点,但 不在父节点及以上节点 调用 findViewById 通过id来查找他们就是没有问题。
Active的 View 根一般是在其在 onCreate函数 中可以通过 setContentView(R.layout.main) 这种形式来设置。
android.view.View中
publicfinalView findViewById (intid)
Since:APILevel1
Lookforachildviewwiththegivenid.Ifthisviewhasthegivenid,returnthisview.
Parameters
id Theidtosearchfor.
Returns
*Theviewthathasthegivenidinthehierarchyornull
因为是 findViewById在是从View及其儿子中查找,所以即使几个layout的XML文件中的View的id号相同的话,只要他们没有相同的父节点 , 或有相同的父亲节点,但 不在父节点及以上节点 调用 findViewById 通过id来查找他们就是没有问题。
一般我都是在layout的XML文件中设置View的id。
比如:
<TextView
android:id="@+id/lable2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:editable="false"/>
<View android:id ="@+id/View01"android:layout_width="wrap_content"android:layout_height="wrap_content"></View>
比如:
<TextView
android:id="@+id/lable2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:editable="false"/>
<View android:id ="@+id/View01"android:layout_width="wrap_content"android:layout_height="wrap_content"></View>