Talk about the interface callback technology in-android

Source: Internet
Author: User
Tags gettext

 

The interface callback technology in Android has a lot of applications, the most common: the Activity (Human-computer interface) UI defines the button, when clicked on the button, the execution of a certain logic.

The following model, described above, describes James ' understanding of the Android Interface callback technology (in combination with previous knowledge and his own practice).

The use of a metaphor is very vividly illustrated: the client has a question to call the server, but the service can not be answered on the spot, the agreement between each other: the service side once the answer, the use of telephone feedback to the client.

There are three principals: client, server, and interface (mode).

The schematic diagram of the interface callback illustrates:

The demo interface is as follows:

The actual results are as follows:

Where Todolistactivity's layout XML file is designed to be populated with fragment tags.

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical"    tools:context= "com.demo.ToDoListActivity" >    <fragment        Android:id= "@+id/fragment_new_item"        android:name= "com.demo.NewItemFragment"        android:layout_width= " Match_parent "        android:layout_height=" wrap_content "/>    <fragment        android:id=" @+id/fragment_ Todo_list "        android:name=" com.demo.ToDoListFragment "        android:layout_width=" Match_parent "        Android : layout_height= "Wrap_content"/></linearlayout>

Demo Source code:

Package Com.demo;import Android.app.activity;import android.app.fragment;import android.os.bundle;import Android.text.textutils;import Android.view.keyevent;import Android.view.layoutinflater;import Android.view.View; Import Android.view.view.onkeylistener;import android.view.viewgroup;import android.widget.edittext;/** * < function description > Fragment defined for new item * * @author Administrator */public class Newitemfragment extends Fragment {private static F    inal String TAG = NewItemFragment.class.getSimpleName ();        /** * < function description > Step 1: Create the specified interface * * @author Administrator */public interface Onnewitemaddedlistener {    Step 2: Create the relevant method in the interface public void onnewitemadded (String newitem); }
//Step 3: Declare the object of the callback interface, interface class object Private Onnewitemaddedlistener Onnewitemaddedlistener;
@Override public void Onattach (activity activity) {Super.onattach (activity); try {//Step 4: Get the parent activity, assign a value to the declared interface object Onnewitemaddedlistener = (Onnewitemaddedlistener) getactivity (); } catch (Exception e) {throw new ClassCastException (activity.tostring () + "must IM Plement Onnewitemaddedlistener "); } }
@Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle Savedinstancesta TE) {//Create or populate fragment UI and return to view object View Contentview = Inflater.inflate (R.layout.new_item_fragment, container, false); Final EditText etcontent = (EditText) contentview. Findviewbyid (r.id.et_content); Etcontent.setonkeylistener (New Onkeylistener () {@Override public boolean onKey (View v, int keycode, KeyEvent event) {if (event.getaction () = = Keyevent.action_down) {LOGUTIL.D (TAG, "Keye Vent. Action_down "); if ((keycode = = Keyevent.keycode_dpad_center) | | (keycode = = Keyevent.keycode_enter)) {LOGUTIL.D (TAG, "Keyevent.keycode_enter"); if (! Textutils.isempty (Etcontent.gettext (). toString ())) {LOGUTIL.D (TAG, "content:>" + Etcontent.gettext (). toString () + "<"); String content = Etcontent.gettext (). toString (); Step 5: Create a scenario that uses the interface callback method onnewitemaddedlistener.onnewitemadded (content); Etcontent.settext (""); return true; }}} return false; } }); return contentview; }}

Activity source code for user and interface UI interaction:

Package Com.demo;import android.app.activity;import android.app.fragmentmanager;import android.os.Bundle; Import Android.widget.arrayadapter;import Com.demo.newitemfragment.onnewitemaddedlistener;import java.util.arraylist;/** * < function description > Step 6: Use in other classes and implement the interface * * @author Administrator */public class todolistactivity Exten DS Activity implements Onnewitemaddedlistener {private static final String TAG = ToDoListActivity.class.getSimp    Lename ();    Private arraylist<string> Mtodoitem; Private arrayadapter<string> Marrayadapter;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Initview (); InitData (); Gets the Fragmentmanager instance that gets the fragment Fragmentmanager Fragmentmanager = Getfragmentmanager () in the XML file; Todolistfragment todolistfragment = (todolistfragment) fragmentmanager. Findfragmentbyid (R.id.fragment_todo _list); Todolistfragment.setlistadapter (Marrayadapter); } private void Initview () {Setcontentview (r.layout.main); } private void InitData () {mtodoitem = new arraylist<string> (); Marrayadapter = new Arrayadapter<string> (this, Android. R.layout.simple_list_item_1, Mtodoitem); } @Override protected void Onresume () {super.onresume (); LOGUTIL.D (TAG, "Onresume ..."); } @Override public void onnewitemadded (String newitem) {///Step 7: Implement the interface and use the functionality of the Mtodoitem.add (0, Newite m); Marrayadapter.notiFydatasetchanged (); }}

The todolistactivity of the demo above implements the interface Onnewitemaddedlistener in the custom newitemfragment, and it is necessary to implement the abstract method in the interface. Through the following methods:

Step 4: Get the parent activity, assign a value to the declared interface object Onnewitemaddedlistener = (Onnewitemaddedlistener) getactivity ();

The todolistactivity instance is passed to Newitemfragment, which is the ability to execute the onnewitemadded () method in the Todolistactivity instance. And this method is covered by the todolistactivity (the actual implementation is the method and logic after the overwrite).

Overview of the interface callback technology in Android:

The Android interface callback method requires a total of 7 steps to be performed:

1. Create the specified interface class and create the relevant method in the interface;

2. Declare an interface object (creating the Interface class object) in a class (typically a server), that is, the interface needs to be used in that class;

3. Assign a value to the interface class object (that is, the server typically instantiates the interface object, using the client's interface instance initialization) in the scenario where the interface object is used (typically the server side);

5. The method in the interface callback (the method of the interface) is used in this scenario (the General Service side);

6. The assignment object in step 4 above (the client passes the instance object, there are many methods: the new interface instance or the client implements the interface) needs to implement the interface;

7. The class (client) in step 6 above, overwrite the method in the interface;

Article derived from: http://www.cnblogs.com/CVstyle/p/6216913.html

Talk about the interface callback technology in-android

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.