在单个活动中动态实现多个片段
2022-09-01 03:58:29
我正在研究片段
我试图实现的用例::
- 我正在使用动态片段
- 我在单个活动中使用三个片段
- 我的目标是在所有三个片段之间进行交流
- 我正在使用片段支持包
每个片段都有一个小部件
-
my_fragment1
有edittext
-
my_fragment2
有button
-
my_fragment3
有TextView
单击文本时,必须显示在button
edittext
textview
到目前为止,我尝试过什么,我已经构建了以下大部分场景
Top_Fragment.java
public class Top_Fragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.my_fragment1, container, false);
return view;
}
}
Middle_Fragment.java
package com.example.deleteme;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class Middle_Fragment extends Fragment{
View view;
Button btn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.my_fragment2, container, false);
btn=(Button) view.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return view;
}
}
Bottom_Fragment.java
public class Bottom_Fragment extends Fragment{
View view;
TextView display_text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.my_fragment3, container,false);
display_text=(TextView) view.findViewById(R.id.editText1);
return view;
}
public void setName(String Name){
display_text.setText("Result::" + Name);
}
}
主要活动.java
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Top_Fragment frg=new Top_Fragment();//create the fragment instance for the top fragment
Middle_Fragment frg1=new Middle_Fragment();//create the fragment instance for the middle fragment
Bottom_Fragment frg2=new Bottom_Fragment();//create the fragment instance for the bottom fragment
FragmentManager manager=getSupportFragmentManager();//create an instance of fragment manager
FragmentTransaction transaction=manager.beginTransaction();//create an instance of Fragment-transaction
transaction.add(R.id.My_Container_1_ID, frg, "Frag_Top_tag");
transaction.add(R.id.My_Container_2_ID, frg1, "Frag_Middle_tag");
transaction.add(R.id.My_Container_3_ID, frg2, "Frag_Bottom_tag");
transaction.commit();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:background="@color/black">
<FrameLayout
android:id="@+id/My_Container_1_ID"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="@color/yellow">
</FrameLayout>
<FrameLayout
android:id="@+id/My_Container_2_ID"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/My_Container_1_ID"
android:background="@color/Orange" >
</FrameLayout>
<FrameLayout
android:id="@+id/My_Container_3_ID"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/My_Container_2_ID"
android:background="@color/purple" >
</FrameLayout>
</RelativeLayout>
my_fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/green" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:textColor="#000000"
android:singleLine="true" >
<requestFocus />
</EditText>
</RelativeLayout>
my_fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/pink">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@color/black"
android:text="Button"
android:textColor="#FFFFFF" />
</RelativeLayout>
my_fragment3.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView"
android:textColor="#000000"
android:textSize="30dp" />
</RelativeLayout>
我的输出如下 ::
我在实现时遇到的问题::
- 我无法将从 获得的值设置为单击
edit text
textview
button
有什么想法吗?