Activity中获取Fragment
// 用于静态创建的Fragment
getSupportFragmentManager().findFragmentById(int id);
// 用于动态创建的Fragment
getSupportFragmentManager().findFragmentByTag(String tag);
// 用于动态创建的Fragment
getSupportFragmentManager().getFragments().get(int index);
Fragment中获取Activity
getActivity();
Fragment中获取另一个Fragment
// 用于静态创建的Fragment
getFragmentManager().findFragmentById(int id);
// 用于动态创建的Fragment
getFragmentManager().findFragmentByTag(String tag);
// 用于动态创建的Fragment
getFragmentManager().getFragments().get(int index);
举个完整的例子:
先看看MainActivity3
package com.gui.gui;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity3 extends BaseActivity {
EditText mEditText;
Button mButton;
int mIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mEditText = findViewById(R.id.edittext);
mButton = findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (mIndex) {
case 0:
LeftFragment fragment0 = (LeftFragment) getSupportFragmentManager().getFragments().get(0);
String value0 = fragment0.mEditText.getText().toString();
mEditText.setText(value0);
break;
case 1:
LeftFragment fragment1 = (LeftFragment) getSupportFragmentManager().getFragments().get(0);
String value1 = mEditText.getText().toString();
fragment1.mEditText.setText(value1);
break;
case 2:
RightFragment fragment2 = (RightFragment) getSupportFragmentManager().getFragments().get(1);
String value2 = fragment2.mEditText.getText().toString();
mEditText.setText(value2);
break;
case 3:
RightFragment fragment3 = (RightFragment) getSupportFragmentManager().getFragments().get(1);
String value3 = mEditText.getText().toString();
fragment3.mEditText.setText(value3);
break;
default:
break;
}
mIndex = (++mIndex) % 4;
}
});
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.left_framelayout, new LeftFragment());
transaction.replace(R.id.right_framelayout, new RightFragment());
transaction.commit();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity3">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="在此输入内容" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="操作按钮" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/left_framelayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/right_framelayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
LeftFragment代码
package com.gui.gui;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class LeftFragment extends Fragment {
EditText mEditText;
Button mButton;
int mIndex;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false);
mEditText = view.findViewById(R.id.edittext);
mButton = view.findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (mIndex) {
case 0:
MainActivity3 activity0 = (MainActivity3) getActivity();
String value0 = activity0.mEditText.getText().toString();
mEditText.setText(value0);
break;
case 1:
MainActivity3 activity1 = (MainActivity3) getActivity();
String value1 = mEditText.getText().toString();
activity1.mEditText.setText(value1);
break;
case 2:
RightFragment fragment2 = (RightFragment) getFragmentManager().getFragments().get(1);
String value2 = fragment2.mEditText.getText().toString();
mEditText.setText(value2);
break;
case 3:
RightFragment fragment3 = (RightFragment) getFragmentManager().getFragments().get(1);
String value3 = mEditText.getText().toString();
fragment3.mEditText.setText(value3);
break;
default:
break;
}
mIndex = (++mIndex) % 4;
}
});
return view;
}
}
<?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/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="left fragment"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="left操作"
android:textAllCaps="false"/>
</LinearLayout>
RightFragment
package com.gui.gui;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class RightFragment extends Fragment {
EditText mEditText;
Button mButton;
int mIndex = 0;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, container, false);
mEditText = view.findViewById(R.id.edittext);
mButton = view.findViewById(R.id.btn);
return view;
}
}
<?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/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="right fragment"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right操作"
android:textAllCaps="false"/>
</LinearLayout>