不同Fragment传数据

不同Fragment传数据,第1张

概述实现点击按钮d出对话框型fra,并从第二fragment获取EditeText的数据返回到第一Fra中并显示。 第一fra1packagecom.example.ourdiary.entries.write_page;23importandroid.os.Bundle;4importandroid.util.Log;5importandroid.view.LayoutInflater;6import

实现点击按钮d出对话框型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             @OverrIDe39             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 supported41                 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 home54      *@time 2021/3/20 0:0555      */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     @OverrIDe63     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     @OverrIDe26     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     @OverrIDe49     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         <TextVIEw14             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         <TextVIEw22             androID:ID="@+ID/tv_fg_diary_write_Title"23             androID:layout_wIDth="match_parent"24             androID:layout_height="wrap_content"/>25 26         <TextVIEw27             androID:ID="@+ID/tv_fg_diary_write_content"28             androID:layout_wIDth="match_parent"29             androID:layout_height="wrap_content"/>30 31   32     <relativeLayout33         androID:layout_alignParentRight="true"34         androID:layout_alignParentBottom="true"35         androID:layout_wIDth="wrap_content"36         androID:layout_height="wrap_content">37 38         <linearLayout39             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.floatingActionbutton46                 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>

 

总结

以上是内存溢出为你收集整理的不同Fragment传数据全部内容,希望文章能够帮你解决不同Fragment传数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1015601.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存