Example-Notepad.ppt
Upcoming SlideShare
Loading in...5
×
 

Example-Notepad.ppt

on

  • 1,083 views

 

Statistics

Views

Total Views
1,083
Views on SlideShare
1,083
Embed Views
0

Actions

Likes
0
Downloads
16
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Example-Notepad.ppt Example-Notepad.ppt Presentation Transcript

  • Android 程式範例 : 記事本 – Notepadv3
  • Android 專案的結構
    • src/ : 原始程式碼
      • DBHelper.java, NoteEdit.java, Notepadv3.java
      • R.java : 自動產生的資源常數檔 , 對映 res/ 中的項目 .
    • Referenced Librarys
      • 引用函式庫 , 通常為 Android.jar
    • assets/ : 資產
      • ???
    • res/ : 資源
      • 包含 drawable, layout, value 等資源 .
    • AndroidManifest.xml : 清單
      • 用來結合所有程式形成一個應用架構
  • Notepadv3 專案結構
  • Android Manifest <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; package=&quot;com.google.android.demo.notepad3&quot;> <application android:icon=&quot;@drawable/icon&quot;> <activity class=&quot;.Notepadv3&quot; android:label=&quot;@string/app_name&quot;> <intent-filter> <action android:value=&quot;android.intent.action.MAIN&quot; /> <category android:value=&quot;android.intent.category.LAUNCHER&quot; /> </intent-filter> </activity> <activity class=&quot;.NoteEdit&quot; android:label=&quot;@string/edit_note&quot;> </activity> </application> </manifest>
  • R.java /* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package com.google.android.demo.notepad3; public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int body=0x7f050001; public static final int confirm=0x7f050002; public static final int text1=0x7f050003; public static final int title=0x7f050000; }
  • R.java public static final class layout { public static final int note_edit=0x7f030000; public static final int notes_list=0x7f030001; public static final int notes_row=0x7f030002; } public static final class string { public static final int app_name=0x7f040000; public static final int body=0x7f040005; public static final int confirm=0x7f040006; public static final int edit_note=0x7f040007; public static final int menu_delete=0x7f040003; public static final int menu_insert=0x7f040002; public static final int no_notes=0x7f040001; public static final int title=0x7f040004; } }
  • res/ res/drawable/Icon.png <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <resources> <string name=&quot;app_name&quot;>Notepad v3</string> <string name=&quot;no_notes&quot;>No Notes Yet</string> <string name=&quot;menu_insert&quot;>Add Note</string> <string name=&quot;menu_delete&quot;>Delete Note</string> <string name=&quot;title&quot;>Title</string> <string name=&quot;body&quot;>Body</string> <string name=&quot;confirm&quot;>Confirm</string> <string name=&quot;edit_note&quot;>Edit Note</string></resources> res/values/strings.xml
  • res/layout <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <TextView id=&quot;@+id/text1&quot; xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot;/> res/layout/note_row.xml <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;wrap_content” android:layout_height=&quot;wrap_content&quot;> <ListView id=&quot;@+id/android:list” android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot;/> <TextView id=&quot;@+id/android:empty” android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content” android:text=&quot;No Notes!&quot;/> </LinearLayout> res/layout/note_list.xml
  • res/layout/note_edit.xml <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot;> <LinearLayout android:orientation=&quot;horizontal&quot; android:layout_width=&quot;fill_parent” android:layout_height=&quot;wrap_content&quot;> <TextView android:layout_width=&quot;wrap_content” android:layout_height=&quot;wrap_content&quot; android:text=&quot;@string/title&quot; /> <EditText id=&quot;@+id/title“ android:layout_width=&quot;wrap_content” android:layout_height=&quot;wrap_content“ android:layout_weight=&quot;1&quot;/> </LinearLayout> <TextView android:layout_width=&quot;wrap_content” android:layout_height=&quot;wrap_content&quot; android:text=&quot;@string/body&quot; /> <EditText id=&quot;@+id/body&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content” android:layout_weight=&quot;1&quot; android:scrollbars=&quot;vertical&quot; /> <Button id=&quot;@+id/confirm“ android:text=&quot;@string/confirm” android:layout_width=&quot;wrap_content” android:layout_height=&quot;wrap_content&quot; /> </LinearLayout>
  • Notepadv3.java – 記事本主程式
    • Notepadv3 是一個 Activity
      • 使用 R.layout. notes_list 作為 ContentView
      • 在 onCreateOptionsMenu (Menu menu) 中建立
        • AddNote 與 Delete Note 項目
      • 在 onMenuItemSelected(int featureId, Item item) 中指定動作
        • case INSERT_ID:
          • createNote();
        • case DELETE_ID:
          • dbHelper.deleteRow(rows.get(getSelection()).rowId);
          • fillData()
      • 在 insert 動作時建立文件 NoteEdit
        • private void createNote() {
          • Intent i = new Intent(this, NoteEdit.class);
          • startSubActivity(i, ACTIVITY_CREATE);
        • }
  • Notepadv3.java – 記事本主程式
      • 編輯動作
        • 在 onListItemClick() 當中,執行 顯示選取文件的動作
        • 由於 編輯與新增文件時,有 startSubActivity, 其結果可在 onActivityResult 中取回,但本程式中,每次返回時只做灰復 list 的動作,
  • OnActivityResult()
    • Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startSubActivity(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, String, Bundle) method.
    • When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER
  • onActivityResult() 的範例
    • public class MyActivity extends Activity  {     ...      static final int PICK_CONTACT_REQUEST = 0;      protected boolean onKeyDown(int keyCode, KeyEvent event)      {          if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {              // When the user center presses, let them pick a contact.              startSubActivity(                  new Intent(Intent.PICK_ACTION, new ContentURI(&quot;content://contacts&quot;)), PICK_CONTACT_REQUEST);             return true;          }          return false;      }      protected void onActivityResult(int requestCode, int resultCode,  String data, Bundle extras)      {          if (requestCode == PICK_CONTACT_REQUEST) {              if (resultCode == RESULT_OK) {                  // A contact was picked.  Here we will just display it to the user.                  startActivity(new Intent(Intent.VIEW_ACTION, data));              }          }      }  }  
  • Intent
    • Intent ( Context packageContext, Class cls)
      • Create an intent for a specific component.
    • Intent ( String action, ContentURI uri)
      • Create an intent with a given action and for a given data url.
  • DBHelper.java
  • NoteEdit.java
  • Activity 的流程概念
    • 注意
      • onPause 時行程會被置換出去
      • onResume 時會再度被置換回來
      • 有可能會被 kill 掉,因此要先記錄資訊
  • startActivity, onActivityResult
    • The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent , which describes the activity to be executed.
    • Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startSubActivity(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, String, Bundle) method.
  •