Add configuration item Page in Android ToDoList

Source: Internet
Author: User

 
 
The task of this article is to add a configuration item Page (Reference) on the simple ToDoList program ). This Reference page is also very simple:

 

This ToDoList now has two pages, the home page can fill in To-Do items, then the manu key pop-up can jump to the Reference page, there is only one checkbox on the Reference page, to confirm whether to save locally (the local storage function is not implemented). There are two buttons on the Reference page to save and return. Click the Save button. The program will store whether the user has selected a local save. Click the return button to go to The ToDoList page.

In this program, there are several points to handle.

1. How to switch between two activities
This is two pages, so we will first think of two layout files, So we created a res/layout/preferences. xml and defined a checkbox and two buttons in this layout. Now the problem is that when I click the manu button in main. xml, it will trigger the onOptionsItemSelected event, so we need to trigger the reference page in this event. How can this be done?

There are two methods:

1. Call setContentView in ToDoListActivity to trigger the presentation of preferences. xml.

2. re-create an Activity Reference class. In ToDoListActivity, Intent is used to trigger the start and draw functions of the Reference class.

What are the differences between the two methods?

The first method is equivalent to using JavaScript in html to make different div meanings. Its advantage is that it is simple. It can do this for simple logic and pages. Its disadvantages are also obvious. When an Activity Controls Multiple layout, all logics are stored in a class at the code level, the ease-of-use and maintainability of the Code are both huge losses. For our application, we choose the second method.

So our onOptionsItemSelected Event code is as follows:

Public boolean onOptionsItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case R. id. manu_reference:
Intent intent = new Intent ();
Intent. setClass (this, Reference. class );
StartActivity (intent );
}
Return true;
}
When the item I trigger in manu is manu_reference (the id has been set in the configuration), then I start the Activity I need.

2 Intent and Activity
Three core components of Android applications: Activity, Service, and Brocast explorer. The messages that these three components interact with each other or internally are called Intent. For example, in our program, two activities need to interact, and Intent needs to be used at this time.

Intent has three usage methods:

Passed to Activity: startActivity (Intent), startActivityForResult ()

Passed to Service: startService (Intent), bindService ()

Pass to Broadcast: sendBroadcast (), sendOrderedBroadcast (), sendStickyBroadcast ()

In this example, in addition to jumping from ToDoListActivity to Reference, you can also jump from Reference to ToDoListActivity (click the return button ).

Button cancelReference = (Button) findViewById (R. id. cancel );
CancelReference. setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View v ){
Intent intent = new Intent ();
Intent. setClass (Reference. this, ToDoListActivity. class );
StartActivity (intent );
}
});
3. configuration item Storage
SharePreferences is used for storing configuration items. SharePreferences provides an interface for you to store and obtain persistent key-value data. Data types that can be persisted include boolean, float, int, long, and string.

To put it simply:

How to create an object:
GetSharePreferences ()

GetPreferences ()

Usage of Data Writing:
1. Use edit () to obtain the write handle

2 call the putXXXX () method

3 call commit

Usage of Data Reading:
GetXXX ()

This is used in this example:

Final SharedPreferences prefs = PreferenceManager. getdefasharsharedpreferences (context );
Boolean isSaveLocal = prefs. getBoolean (IS_SAVE_LOCAL, false );
 
...
 
Editor editor = prefs. edit ();
Editor. putBoolean (IS_SAVE_LOCAL, isChecked );
Editor. commit ();

Related Article

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.