如何在Fragment中使用findViewById?

How to use findViewById in Fragment?

本示例演示有关如何在Fragment中使用findViewById

步骤1 ?在Android Studio中创建一个新项目,转到文件?新建项目,并填写所有必需的详细信息以创建一个新项目。

第2步 ?将以下代码添加到res / layout / activity_main.xml。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version ="1.0" encoding ="utf-8"?>
<LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
 android:orientation ="vertical"
 android:layout_width ="match_parent"
 android:layout_height ="match_parent">
 <LinearLayout
   android:id ="@+id/linearlayout01"
   android:layout_width ="fill_parent"
   android:layout_height ="fill_parent"
   android:background ="#ccc"
   android:layout_weight ="1"
   android:orientation ="vertical">
   <fragment android:name ="com.example.myapplication.FirstFragment"
    android:id ="@+id/frag_1"
    android:layout_width ="fill_parent"
    android:layout_height ="fill_parent" />
 </LinearLayout>
 <LinearLayout
   android:id ="@+id/linearlayout02"
   android:layout_width ="fill_parent"
   android:layout_height ="fill_parent"
   android:layout_weight ="1"
   android:background ="#eee"
   android:orientation ="vertical">
   <fragment android:name ="com.example.myapplication.SecondFragment"
    android:id ="@+id/frag_2"
    android:layout_width ="fill_parent"
    android:layout_height ="fill_parent" />
 </LinearLayout>
</LinearLayout>

在上面的代码中,我们截取了两个片段。

第3步?将以下代码添加到src / MainActivity.java中

1
2
3
4
5
6
7
8
9
10
<?xml version ="1.0" encoding ="utf-8"?>
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
 }
}

第四步 ?将以下代码添加到src / FirstFragment.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version ="1.0" encoding ="utf-8"?>
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FirstFragment extends Fragment {
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment, null);
   TextView but = (TextView) root.findViewById(R.id.text);
   return root;
 }
}

第四步 ?将以下代码添加到src / SecondFragment.java中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version ="1.0" encoding ="utf-8"?>
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SecondFragment extends Fragment {
 TextView textView;
 View view;
 @Nullable
 @Override
 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
   view = inflater.inflate(R.layout.fragment, container, false);
   return view;
 }
}

让我们尝试运行您的应用程序。我假设您已将实际的Android Mobile设备与计算机连接。要从android studio运行该应用,请打开您项目的活动文件之一,然后从工具栏中单击运行 icon。选择您的移动设备作为选项,然后检查将显示默认屏幕的移动设备–

点击这里下载项目代码