如何在片段中使用 findViewById?


本示例演示如何在片段中使用 findViewById

步驟 1 − 在 Android Studio 中建立新專案,轉到“檔案 ⇒ 新專案”,然後填寫所有必填資訊以建立新專案。

步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml。

<?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

<?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);
   }
}

步驟 4 − 將以下程式碼新增到 src/ FirstFragment.java

<?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;
   }
}

步驟 4 − 將以下程式碼新增到 src/ SecondFragment.java

<?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 移動裝置與計算機連線。要從 Android Studio 執行應用,請開啟專案的一個活動檔案,然後單擊工具欄中的執行  圖示。選擇你的移動裝置作為選項,然後檢查移動裝置顯示的預設螢幕 –

點選此處下載專案程式碼

更新於:2019 年 7 月 30 日

5K+ 次瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始操作
廣告
© . All rights reserved.