1. How to Make the UI
FirstCreate an Android Project:
1) Application Name:SimpleNotePad
2) Minimum Required SDK: API 14)
Then click it until it is finished.
1.1 implement homepage Activity
We first implement the homepage Activity: this Activity is composed of two parts:
·Top: The top is an ActionBar (similar to the design of a webpage, many of the top pages are logo and website titles. Currently, Android apps are more popular in the style of having an ActionBar on the top, display the App icon, title, and available operations, for example:
Where
1) Logo,
2) It is the title of the App. You can click the triangle on the right of the title to pull out some menus;
3) is an operation that can be done,
4) It is called Action Overflow. If you provide a lot of operations, but you cannot put them in 3), you can put them in 4.
In the homepage Activity of the APP, we only display the Logo and title in the ActionBar, andAdd notesIcon. We will introduce how to add the Action icon in the Code and respond to the click icon event.
Note: ActionBar is a new feature introduced by Android 3.0. The corresponding SDK version is 11. Therefore, an Android App must use ActionBar, and its Target SDK version must be at least 11.
·Bottom: Display all tabs. Here we make the Home Page Activity inherit from the provided by the system.ListActivityIn this way, you only need to bind the data to this homepage Activity (details are provided below ).
1.1.1 ActionBar
Through the Create Project Wizard, In the CreateActivity step, the selected BlankActivity already has an ActionBar. Do not believe it. Check the small text below the dialog box! (Because the SDK version we selected is later than 11)
The Logo and title of the Activity created by the Wizard do not meet our requirements. We need to modify it.
First, we need to modify the default icon because it is a convenience App. To make it more similar, we need to use a suitable icon, for example:
Steps for modifying the Logo:
1) Open the example path of the NotePad that comes with the sdk (C: \ ProgramFiles \ Android \ android-sdk \ samples \ android-17 \ NotePad \ res \ drawable on my machine ),
2)beiapp_notes.png,
3) then go to our project res/drawable(if there is no drawable, create a new project, select drawableand paste app_notes.png, then the file will be under the drawable directory.
4) Open AndroidManifest. xml and modify android: icon under <application>:
Note: In AndroidManifest. in xml, the icon/label attribute in the <application> label defines the Logo and title of the app respectively. In the <activity> label, I can also define the icon and/or label, if no definition is defined in <activity>, the Logo and title of the Activity are defined in <application>. Otherwise, use your own Logo and title, for example:
In this example, the MainActivity title is @ string/app_title, while the Logo is @ drawable/app_notes.
We want all the activities in this App to use the same Logo, so we only define the icon in <application>, but not every Activity. On the contrary, for the title of the activity, each activity is different: For the homepage Activity, we use the title "Notes":
1) Add in values/strings. xml
<Stringname = "app_title"> Notes </string>
2) modify the property of <activity> In AndroidManifes. xml:
<Activity
Android: name = "com. example. simplenotepad. MainActivity"
Android: label = "@ string/app_title">
...
</Activity>
After the Logo and title are adjusted, we need to add the "Add new tag" icon in the ActionBar. Note that through the new project wizard, this code exists in MainActivity. java:
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }
In additionThe main. xml file is automatically generated under res/menu.In this file, we can define the menu item (also called Action) to be added to the ActionBar. When the Activity is started, it will be called to onCreateOptionsMenu, which will put main. actions defined in xml are displayed in ActionBar.
Modify main. xml as follows:
1) rename it main_menu.xml: Select main. xml, press F2, and enter main_menu.xml (note that onCreateOptionsMenu will be automatically updated after the name is changed ).
2) Update main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_add" android:icon="@drawable/ic_menu_compose" android:title="@string/menu_add" android:alphabeticShortcut='a' android:showAsAction="always" /></menu>
3) Add the following in res/values/strings. xml:
<string name="menu_add">Newnote</string>
4. Use the cursor to modify the logostep, from the SDK example, beiic_menu_compose.png to/res/drawable.
Run the program to see the effect:
The above main_menu.xml defines an Action, that is, "Add a new signature ".ShowAsAction attributesIt is worth noting thatAlways, That is, this Action is always displayed; if it isIfRoomIn most cases, this Action may be hidden in Action Overflow.
Response Action click: Add the following code to MainActivity. java:
import android.widget.Toast; @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_add: Toast.makeText(this, "Menu add new note clicked.", Toast.LENGTH_LONG).show(); return true; default: return super.onOptionsItemSelected(item); } }
We useToast(A Message Display Method in Android) to display the click effect. We will enable the Activity to edit the note after clicking:
1.2 create a notebook Activity
First, select the SimpleNotePad project, right-click and choose New => Other... => Android Activity (or click the first New icon in the Toolbar), click Next, select Blank Activity, and click Next. Modify the information of the New Activity as follows:
Click Next to see the update to be executed by Eclipse. Click Finish to create the Activity.
Note: The updates executed by Eclipse for creating a new Activity include:
1) information about the new Activity is added to AndroidManifest. xml,All activities in an App must be defined in AndroidManifest. xml.!
2) added a new Activity title and content string in res/values/strings. xml.
3) create note_editor.xml IN res/menu, which contains the ActionBar information of the new Activity.
4) activity_note_editor.xml is created in res/layout, which is the UI definition of the new Activity.
Open res/layout/activity_note_editor.xml and update its content:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".NoteEditorActivity" > <TextView android:id="@+id/titleLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/note_title"/> <EditText android:id="@+id/noteTitleEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/titleLabel"/> <TextView android:id="@+id/contentLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/noteTitleEditText" android:text="@string/note_content" /> <EditText android:id="@+id/noteContentEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/contentLabel"/></RelativeLayout>
The new tab Activity contains two EditText controls, which allow users to enter the tab title and content.
Next, we will set the ActionBar for the new notebook Activity, and add the "save" and "cancel" actions in it:
1) Rename res/menu/note_editor.xml to note_editor_menu.xml.
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_save" android:icon="@drawable/ic_menu_save" android:alphabeticShortcut='s' android:title="@string/menu_save" android:showAsAction="ifRoom|withText" /> <item android:id="@+id/menu_delete" android:icon="@drawable/ic_menu_delete" android:title="@string/menu_delete" android:showAsAction="ifRoom|withText" /></menu>
2. Copy ic_menu_save.pngand ic_menu_delete.png from the sdk sample to res/drawable.
3) IN res/values/strings. xml, add
<string name="menu_save">Save</string> <string name="menu_delete">Delete</string>
1.3 start the new note Activity from the homepage Activity
Intent is required to jump from Activity to another Activity.: Open MainActivity. java and update onOptionsItemSelected:
import android.content.Intent; @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_add: Intent intent = new Intent(this, NoteEditorActivity.class); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); } }
About Intent:Intent is a message.It is mainly used to start another Activity from one Activity, including:
1) start another Activity in this App, or
2) Start an Activity in another App in the Android system, such as starting a camera.
Here we use:
Intentintent = new Intent(this, NoteEditorActivity.class); startActivity(intent);
First, an Intent is constructed. The constructor contains a Context object (this is an Activity, and the Activity itself inherits from the Content). The other is the information of the Activity to be started.
Run the App and click "add note" Action on the home page to bring up the new note Activity:
So far, we have completed two activities, and implemented the ActionBar and jump from the homepage Activity to the new note Activity. We only involved the UI and did not involve the processing of the note data. Next, let's start with the data storage and reading sections.
The idea is to start from creating a notebook Activity. After editing the notebook content in it, click "save" Action. The data should be saved to the database and then returned to the homepage Activity, at this time, the homepage Activity should display the new tab information. After this link passes, we have a bottom in our hearts.
Next we will explain how to access the signature data from the SQLite database.