Step 1: Define an interface in fragment and make sure that our container activity implements this interface:
Public interface ontestlistener {
Public void ontest (string Str);
}< br> @ override
Public void onattach (activity) {
super. onattach (activity);
// This method is used to check whether the current activity container inherits this interface, if no exception is thrown,
try {
mcallback = (ontestlistener) activity;
} catch (classcastexception E) {
throw new classcastexception (activity. tostring ()
+ "must implement onheadlineselectedlistener ");
}< br> }< br>
Step 2: implement this interface in our container activity and implement the methods in the interface.
Public class mainactivity extends activity implements ontestlistener {
......
@ Override
Public void ontest (string Str ){
Text. settext (STR );
}
Step 3: Call the methods declared in the interface when we perform certain operations in fragment.
@ Override
Public void onclick (view v ){
Mcallback. ontest (edit. gettext (). tostring ());
}
Link to the complete code: http://download.csdn.net/detail/huangyabin001/7560291click to open the link