实现点击按钮弹出对话框型fra,并从第二fragment获取EditeText的数据返回到第一Fra中并显示。
第一fra
1 package com.example.ourdiary.entries.write_page; 2 3 import android.os.Bundle; 4 import android.util.Log; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.TextView; 9 10 import androidx.annotation.NonNull; 11 import androidx.annotation.Nullable; 12 import androidx.fragment.app.Fragment; 13 import androidx.fragment.app.FragmentResultListener; 14 15 import com.example.ourdiary.R; 16 17 import com.example.ourdiary.entries.DiaryActivity; 18 import com.google.android.material.floatingactionbutton.FloatingActionButton; 19 20 21 public class DiaryWriteFragment extends Fragment { 22 23 private DiaryActivity activity; 24 FloatingActionButton fab_diary_write; 25 TextView tv_title,tv_content; 26 String result_title,result_content; 27 28 29 public DiaryWriteFragment(DiaryActivity activity) { 30 this.activity = activity; 31 } 32 33 34 35 public void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 getParentFragmentManager().setFragmentResultListener("requestDiaryInputKey", this, new FragmentResultListener() { 38 @Override 39 public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle bundle) { 40 // We use a String here, but any type that can be put in a Bundle is supported 41 result_title = bundle.getString("bundle_title"); 42 result_content = bundle.getString("bundle_content"); 43 // Log.d("test",result_title); 44 // Log.d("test",result_content); 45 tv_title.setText(result_title); 46 tv_content.setText(result_content); 47 } 48 }); 49 } 50 51 /** 52 * 53 *@author home 54 *@time 2021/3/20 0:05 55 */ 56 public View onCreateView(LayoutInflater inflater, ViewGroup container, 57 Bundle savedInstanceState) { 58 View rootView = inflater.inflate(R.layout.fg_diary_write,container,false); 59 return rootView; 60 } 61 62 @Override 63 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 64 tv_title = view.findViewById(R.id.tv_fg_diary_write_title); 65 tv_content = view.findViewById(R.id.tv_fg_diary_write_content); 66 fab_diary_write = view.findViewById(R.id.fab_diary_write); 67 68 fab_diary_write.setOnClickListener(view1 -> { 69 DiaryWriteInputFragment diaryWriteInputFragment = new DiaryWriteInputFragment(); 70 diaryWriteInputFragment.show(activity.getSupportFragmentManager(),"diaryInputDialogFragment"); 71 }); 72 } 73 74 }
第二fra
1 package com.example.ourdiary.entries.write_page; 2 3 import android.app.Dialog; 4 import android.content.DialogInterface; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.widget.Button; 10 import android.widget.EditText; 11 12 import androidx.annotation.NonNull; 13 import androidx.annotation.Nullable; 14 import androidx.appcompat.app.AlertDialog; 15 import androidx.fragment.app.DialogFragment; 16 17 import com.example.ourdiary.R; 18 import com.google.android.material.floatingactionbutton.FloatingActionButton; 19 20 public class DiaryWriteInputFragment extends DialogFragment { 21 22 EditText et_title,et_content; 23 FloatingActionButton fab_save; 24 25 @Override 26 public Dialog onCreateDialog(Bundle savedInstanceState) { 27 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 28 29 LayoutInflater inflater = getActivity().getLayoutInflater(); 30 View view = inflater.inflate(R.layout.fg_diary_write_input, null); 31 builder.setView(view); 32 et_title = view.findViewById(R.id.et_diary_write_title); 33 et_content = view.findViewById(R.id.et_diary_write_content); 34 fab_save = view.findViewById(R.id.fab_diary_input_save); 35 36 37 fab_save.setOnClickListener(v -> { 38 // Log.d("test","buttonsave!"); 39 Bundle result = new Bundle(); 40 result.putString("bundle_title", String.valueOf(et_title.getText())); 41 result.putString("bundle_content", String.valueOf(et_content.getText())); 42 getParentFragmentManager().setFragmentResult("requestDiaryInputKey", result); 43 dismiss();//使此fragment消失 44 }); 45 return builder.create(); 46 } 47 48 @Override 49 public void onCancel(@NonNull DialogInterface dialog) { 50 super.onCancel(dialog); 51 } 52 53 }
第一fra的xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:orientation="vertical"> 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:orientation="vertical"> 12 13 <TextView 14 android:id="@+id/tv_fg_diary_write" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="this is write fragment!" 18 android:textSize="30sp" 19 android:paddingLeft="10dp"/> 20 21 <TextView 22 android:id="@+id/tv_fg_diary_write_title" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content"/> 25 26 <TextView 27 android:id="@+id/tv_fg_diary_write_content" 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content"/> 30 31 32 <RelativeLayout 33 android:layout_alignParentRight="true" 34 android:layout_alignParentBottom="true" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content"> 37 38 <LinearLayout 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 android:orientation="vertical" 42 android:paddingRight="20dp" 43 android:paddingBottom="20dp"> 44 45 <com.google.android.material.floatingactionbutton.FloatingActionButton 46 android:backgroundTint="@color/light_blue" 47 android:id="@+id/fab_diary_write" 48 android:layout_width="wrap_content" 49 android:layout_height="wrap_content" 50 android:contentDescription="add" 51 android:tint="@color/color_White" 52 android:src="@drawable/iv_memo_item_add_icon"/> 53 </LinearLayout> 54 </RelativeLayout> 55 56 </RelativeLayout>
第二fra的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"> <EditText android:id="@+id/et_diary_write_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="enter your title here!" android:textColorHint="@color/color_black"/> <EditText android:id="@+id/et_diary_write_content" android:layout_width="match_parent" android:layout_height="200dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:hint="enter your content here!" android:textColorHint="@color/color_black"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.floatingactionbutton.FloatingActionButton android:backgroundTint="@color/light_blue" android:id="@+id/fab_diary_input_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="add" android:tint="@color/color_White" android:layout_centerHorizontal="true" android:src="@drawable/iv_diary_save"/> </RelativeLayout> </LinearLayout>