2、创建一个应用程序,设置上下两个布局A、B。B布局内放置两个按钮(text属性的值为ActivityButton1、ActivityButton2)和一个文本框(text属性的值为ActivityTextView)。 (1)在点击两个按钮时,能够动态加载两个Fragment(类名为AFragment、BFragment)到A布局中。 (2)在AFragment中添加按钮(text属性的值为FragmentButton)和文本框(text属性的值为FragmentTextView)。当点击FragmentButton时,能够实现将Activity中的文本传递到FragmentTextView,同时将AFragment中的文本传递到ActivityTextView中。
时间: 2024-01-23 17:03:10 浏览: 57
好的,这是一个关于Android开发的问题。下面是代码实现:
1. 在布局文件activity_main.xml中,设置上下两个布局A、B,并在B布局中放置两个按钮和一个文本框:
```
<?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/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ActivityButton1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ActivityButton2" />
<EditText
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ActivityTextView" />
</LinearLayout>
</LinearLayout>
```
2. 创建两个Fragment类AFragment、BFragment,并在布局文件fragment_a.xml中添加按钮和文本框:
AFragment.java
```
public class AFragment extends Fragment {
private EditText mFragmentTextView;
public AFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_a, container, false);
mFragmentTextView = view.findViewById(R.id.fragmentTextView);
Button fragmentButton = view.findViewById(R.id.fragmentButton);
fragmentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取Activity中的文本
EditText activityTextView = getActivity().findViewById(R.id.textview);
String activityText = activityTextView.getText().toString();
//将Activity中的文本传递到FragmentTextView中
mFragmentTextView.setText(activityText);
//获取AFragment中的文本
String fragmentText = mFragmentTextView.getText().toString();
//将AFragment中的文本传递到ActivityTextView中
activityTextView.setText(fragmentText);
}
});
return view;
}
}
```
fragment_a.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">
<Button
android:id="@+id/fragmentButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FragmentButton" />
<EditText
android:id="@+id/fragmentTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FragmentTextView" />
</LinearLayout>
```
BFragment.java
```
public class BFragment extends Fragment {
public BFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_b, container, false);
}
}
```
fragment_b.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>
```
3. 在MainActivity中,实现点击按钮时动态加载Fragment:
```
public class MainActivity extends AppCompatActivity {
private Button mButton1;
private Button mButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1 = findViewById(R.id.button1);
mButton2 = findViewById(R.id.button2);
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//动态加载AFragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, new AFragment());
transaction.commit();
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//动态加载BFragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, new BFragment());
transaction.commit();
}
});
}
}
```
这样,就完成了创建一个应用程序,设置上下两个布局A、B。B布局内放置两个按钮(text属性的值为ActivityButton1、ActivityButton2)和一个文本框(text属性的值为ActivityTextView)。在点击两个按钮时,能够动态加载两个Fragment(类名为AFragment、BFragment)到A布局中。在AFragment中添加按钮(text属性的值为FragmentButton)和文本框(text属性的值为FragmentTextView)。当点击FragmentButton时,能够实现将Activity中的文本传递到FragmentTextView,同时将AFragment中的文本传递到ActivityTextView中。
阅读全文