Android Learning Note fragment communication

Source: Internet
Author: User

1. Layout files

Mian.xml

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:id= "@+id/linearlayout"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 7     <EditText8         Android:id= "@+id/edittext1"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" /> One  A     <Button -         Android:id= "@+id/button1" - Android:layout_width= "Match_parent" the Android:layout_height= "Wrap_content" /> -  - </LinearLayout>
View Code

Fragment.xml

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Match_parent"3 Android:layout_height= "Match_parent"4 android:orientation= "vertical" >5     6     <TextView7         Android:id= "@+id/textview1"8 Android:layout_width= "Match_parent"9 Android:layout_height= "Wrap_content"/>Ten      One  A </LinearLayout>
View Code

2.java Code

Mainactivity.class

1  Public classMainactivityextendsActionbaractivityImplementsmylistener{2 3     PrivateButton B1;4     PrivateEditText Et1;5 6 @Override7     protected voidonCreate (Bundle savedinstancestate) {8 9         Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.main); One  AEt1 =(EditText) Findviewbyid (R.ID.EDITTEXT1); -B1 =(Button) Findviewbyid (r.id.button1); -B1.setonclicklistener (NewOnclicklistener () { the  - @Override -              Public voidOnClick (View v) { -                 //TODO auto-generated Method Stub +String Text =Et1.gettext (). toString (); -Bundle bundle =NewBundle (); +Myfragment myfragment =Newmyfragment (); ABundle.putstring ("name", text); at myfragment.setarguments (bundle); -Fragmentmanager Fragmentmanager =Getfragmentmanager (); -Fragmenttransaction fragmenttransaction =fragmentmanager.begintransaction (); -Fragmenttransaction.add (R.id.linearlayout, Myfragment, "Myfragment"); -Fragmenttransaction.addtobackstack (NULL); - fragmenttransaction.commit (); inToast.maketext (mainactivity. This, "Send data to fragment" +text, Toast.length_short). Show (); -  to             } +         }); -     } the  * @Override $      Public voidThank (String code) {Panax Notoginseng         //TODO auto-generated Method Stub -Toast.maketext (mainactivity. This, "successfully received" +code+ "polite", Toast.length_short). Show (); the     } +  A  the}
View Code

Myfragment

1  Public classMyfragmentextendsfragment{2     3     PrivateTextView TV1;4     PrivateString code = "Thanks";5     PrivateMyListener Listener;6     7      Public Interfacemylistener{8         9          Public voidThank (String code);Ten              One     } A      - @Override -      Public voidOnattach (activity activity) { the         //TODO auto-generated Method Stub -Listener =(MyListener) activity; -         Super. Onattach (activity); -     } +      - @Override +      PublicView Oncreateview (layoutinflater inflater, ViewGroup container, A Bundle savedinstancestate) { at         //TODO auto-generated Method Stub -View view = Inflater.inflate (R.layout.fragment, container,false); -TV1 =(TextView) View.findviewbyid (r.id.textview1); -String text = getarguments (). Get ("name") + ""; - tv1.settext (text); -Toast.maketext (Getactivity (), "received successfully" +text, Toast.length_short). Show (); inToast.maketext (Getactivity (), "Send to activity" +text, Toast.length_short). Show (); - Listener.thank (code); to         returnview; +     } -  the}
View Code

Communication for fragment of the Android learning note

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.