findViewById()有什么用

What is the use of findViewById()

我是Android开发新手,有很多困惑和疑惑,需要有经验的人真诚的帮助。

findViewById()

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

所以基本上您需要将 ID 传递给函数,它会检查 id。这将 return 具有给定 id

的视图

它将提供对视图的引用

我。即:

setContentView(R.layout.webview_page);
WebView web = (WebView) findViewById(R.id.myWebView);

将使您能够在 setContentView()

中指定的 xml 布局中引用名为 myWebView 的 WebView

需要在xml布局文件中为WebView分配相应的id属性:

android:id="@+id/myWebView"