Demo
MainActivity.java
package com.sjcoding.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements FragmentA.FragmentAListener, FragmentB.FragmentBListener {
private FragmentA fragmentA;
private FragmentB fragmentB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentA = new FragmentA();
fragmentB = new FragmentB();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_a, fragmentA)
.replace(R.id.container_b, fragmentB)
.commit();
}
@Override
public void onInputASent(CharSequence input) {
fragmentB.updateEditText(input);
}
@Override
public void onInputBSent(CharSequence input) {
fragmentA.updateEditText(input);
}
}
Fragment A, B 파일 2개 같은 구조로 만들면됨
package com.sjcoding.myapplication;
import android.content.Context;
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 FragmentA extends Fragment {
private FragmentAListener listener;
private EditText editText;
private Button btnOk;
public interface FragmentAListener {
void onInputASent(CharSequence input);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_a, container, false);
editText = v.findViewById(R.id.eidt_text);
btnOk = v.findViewById(R.id.btn_ok);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CharSequence input = editText.getText();
listener.onInputASent(input);
}
});
return v;
}
public void updateEditText(CharSequence newText) {
editText.setText(newText);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if(context instanceof FragmentAListener) {
listener = (FragmentAListener) context;
} else {
throw new RuntimeException(context.toString() + " must implement FragmentAListener");
}
}
@Override
public void onDetach() {
super.onDetach();
listener = null;
}
}
activity_main.xml
<?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:id="@+id/container"
tools:context=".MainActivity"
android:orientation="vertical">
<FrameLayout
android:id="@+id/container_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/container_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
fragment_a.xml,fragment_b.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="wrap_content"
android:background="@android:color/holo_green_dark"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/eidt_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok" />
</LinearLayout>
'System Programming > Android' 카테고리의 다른 글
[Android/Java] 갤러리 열고 그리기 MediaStore, bitmap (0) | 2021.06.29 |
---|---|
[Android/Java] Fragment Bottom Navigation view (0) | 2021.06.28 |
[Android/Java] BlankFragment (0) | 2021.06.28 |
[Android/Kotlin] Live Data (0) | 2021.06.28 |
[Android/Java] naver map api (0) | 2021.06.19 |