Android's ToDoList detailed

Source: Internet
Author: User

ToDoList is an Android starter program that contains two controls, EditText (edit text), ListView (List view), placed in LinearLayout (linear layout);

Need to rewrite the listening key (Setonkeylistener) and use the adapter (Adapter) to correlate;

1. Code for the main interface (Activity_to_do_list.xml):

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:orientation=" vertical "android:layout_width=" match_parent "Android:layout_h" eight= "Match_parent" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen Activity_horizontal_margin "android:paddingtop=" @dimen/activity_vertical_margin "android:paddingbottom=" @dimen
        Activity_vertical_margin "tools:context=" mzx.spike.todolist.app.ToDoListActivity "> <edittext
        Android:id= "@+id/myedittext" android:layout_width= "match_parent" android:layout_height= "Wrap_content"   
      
    android:hint= "@string/additemhint" android:contentdescription= "@string/additemcontentdescription"/> <listview android:id= "@+id/mylistview" android:layout_width= "Match_parent" Androi d:layout_height= "Wrap_content"/> </Linearlayout> 

Location: Res->layout->activity_to_do_list.xml

Attention:

1. All controls, you must specify the Layout_width (control width), layout_height (control height), two properties, the Android system will make the corresponding match;

2. Id attribute, is to register in the R file, in the program logic (Java file) use, so use "@+id", "+" means added meaning;

3. LinearLayout control (linear), need to indicate the orientation (direction), so that its internal control orderly arrangement;

4. Layout matching two commonly used attributes: Match_parent (matching the parent control, fill), wrap_content (enclosing the content, according to the content of the minimized);

2. Code for string (strings.xml):

<?xml version= "1.0" encoding= "Utf-8"?>  
<resources>  
      
    <string name= "App_name" >ToDoList< /string>  
    <string name= "Additemhint" >new to do item</string> <string  
    " Additemcontentdescription ">new to do item</string>  
    <string name=" action_settings ">Settings</" String>  
      
</resources>

Location: Res->values->string.xml

Attention:

Using the form of strings, you can separate the presentation layer from the application logic layer and extend other languages;

3. Code for Program Logic (Todolistactivity.java):

Package Mzx.spike.todolist.app;  
Import Android.os.Bundle;  
Import android.support.v7.app.ActionBarActivity;  
Import android.view.KeyEvent;  
Import Android.view.Menu;  
Import Android.view.MenuItem;  
Import Android.view.View;  
Import Android.widget.ArrayAdapter;  
Import Android.widget.EditText;  
      
Import Android.widget.ListView;  
      
      
Import java.util.ArrayList; public class Todolistactivity extends actionbaractivity {@Override protected void onCreate (Bundle savedi  
        Nstancestate) {super.oncreate (savedinstancestate);  
      
        Setcontentview (r.layout.activity_to_do_list); Get a reference to the UI component//view this column more highlights: Http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/ListView Mylistview = (listvie  
        W) Findviewbyid (R.id.mylistview); Final EditText Myedittext = (edittext) Findviewbyid (R.id.myedittext); Final expression constants//Get references to UI components final arraylist<string> Todoitems = new Arraylist<  
      
        String> ();  
        Create Arrayadapter to speak array binding to ListView final arrayadapter<string> AA; AA = new Arrayadapter<string> (this, Android.  
      
        R.layout.simple_list_item_1, Todoitems);  
      
        Binding the Arrayadapter to the ListView mylistview.setadapter (AA);
            Listen for the Myedittext enter key Myedittext.setonkeylistener (new View.onkeylistener () {@Override public Boolean OnKey (view view, int i, keyevent keyevent) {if (keyevent.getaction () = = Keyevent.action _down) if ((i = = Keyevent.keycode_dpad_center) | | (i = = Keyevent.keycode_enter))  
                        {todoitems.add (0, Myedittext.gettext (). toString ());  
                        Aa.notifydatasetchanged ();  
                        Myedittext.settext ("");  
                    return true;  
            return false;  
    }  
        }); } @OverrIDE public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the ' menu; this adds items to T  
        He action Bar if it is present.  
        Getmenuinflater (). Inflate (r.menu.to_do_list, menu);  
    return true; @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar Item CLI Cks here. The action bar would//automatically handle clicks on the Home/up button, so long//As you specify a  
        Parent activity in Androidmanifest.xml.  
        int id = item.getitemid ();  
        if (id = = r.id.action_settings) {return true;  
    return super.onoptionsitemselected (item); }  
      
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

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.