编辑:::请参考下面的答案......
题:::
我在Android中使用Fragments非常新,我完全搞砸了.
我只是想构建一个使用Fragments的简单示例应用程序.我的情况是,我的主要活动中有两个片段.第一个片段有一个edittext和一个按钮.第二个片段有一个textview.当我在edittext中输入名称并单击按钮时,第二个片段中的textview应显示在第一个片段的edittext中输入的名称.
我正在使用片段的静态分配(在XML中分配片段).
请参考下面的XML文件和代码......
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/fragment_content_1"
android:name="com.example.fragmentexample.fragment_fragment_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</fragment>
<fragment
android:id="@+id/fragment_content_2"
android:name="com.example.fragmentexample.fragment_fragment_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- Preview: layout=@layout/fragment_basic -->
</fragment>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
fragment_fragment_1.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edtxtPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSayHi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say Hi" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
fragment_fragment_2.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I will say Hi" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Java文件::: …