Your SlideShare is downloading. ×
0
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Example-Notepad.ppt
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Example-Notepad.ppt

851

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
851
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Android 程式範例 : 記事本 – Notepadv3
  • 2. Android 專案的結構 <ul><li>src/ : 原始程式碼 </li></ul><ul><ul><li>DBHelper.java, NoteEdit.java, Notepadv3.java </li></ul></ul><ul><ul><li>R.java : 自動產生的資源常數檔 , 對映 res/ 中的項目 . </li></ul></ul><ul><li>Referenced Librarys </li></ul><ul><ul><li>引用函式庫 , 通常為 Android.jar </li></ul></ul><ul><li>assets/ : 資產 </li></ul><ul><ul><li>??? </li></ul></ul><ul><li>res/ : 資源 </li></ul><ul><ul><li>包含 drawable, layout, value 等資源 . </li></ul></ul><ul><li>AndroidManifest.xml : 清單 </li></ul><ul><ul><li>用來結合所有程式形成一個應用架構 </li></ul></ul>
  • 3. Notepadv3 專案結構
  • 4. 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>
  • 5. 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; }
  • 6. 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; } }
  • 7. 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
  • 8. 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
  • 9. 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>
  • 10. Notepadv3.java – 記事本主程式 <ul><li>Notepadv3 是一個 Activity </li></ul><ul><ul><li>使用 R.layout. notes_list 作為 ContentView </li></ul></ul><ul><ul><li>在 onCreateOptionsMenu (Menu menu) 中建立 </li></ul></ul><ul><ul><ul><li>AddNote 與 Delete Note 項目 </li></ul></ul></ul><ul><ul><li>在 onMenuItemSelected(int featureId, Item item) 中指定動作 </li></ul></ul><ul><ul><ul><li>case INSERT_ID: </li></ul></ul></ul><ul><ul><ul><ul><li>createNote(); </li></ul></ul></ul></ul><ul><ul><ul><li>case DELETE_ID: </li></ul></ul></ul><ul><ul><ul><ul><li>dbHelper.deleteRow(rows.get(getSelection()).rowId); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>fillData() </li></ul></ul></ul></ul><ul><ul><li>在 insert 動作時建立文件 NoteEdit </li></ul></ul><ul><ul><ul><li>private void createNote() { </li></ul></ul></ul><ul><ul><ul><ul><li>Intent i = new Intent(this, NoteEdit.class); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>startSubActivity(i, ACTIVITY_CREATE); </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  • 11. Notepadv3.java – 記事本主程式 <ul><ul><li>編輯動作 </li></ul></ul><ul><ul><ul><li>在 onListItemClick() 當中,執行 顯示選取文件的動作 </li></ul></ul></ul><ul><ul><ul><li>由於 編輯與新增文件時,有 startSubActivity, 其結果可在 onActivityResult 中取回,但本程式中,每次返回時只做灰復 list 的動作, </li></ul></ul></ul>
  • 12. OnActivityResult() <ul><li>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. </li></ul><ul><li>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 </li></ul>
  • 13. onActivityResult() 的範例 <ul><li>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));              }          }      }  }   </li></ul>
  • 14. Intent <ul><li>Intent ( Context packageContext, Class cls) </li></ul><ul><ul><li>Create an intent for a specific component. </li></ul></ul><ul><li>Intent ( String action, ContentURI uri) </li></ul><ul><ul><li>Create an intent with a given action and for a given data url. </li></ul></ul>
  • 15. DBHelper.java
  • 16. NoteEdit.java
  • 17. Activity 的流程概念 <ul><li>注意 </li></ul><ul><ul><li>onPause 時行程會被置換出去 </li></ul></ul><ul><ul><li>onResume 時會再度被置換回來 </li></ul></ul><ul><ul><li>有可能會被 kill 掉,因此要先記錄資訊 </li></ul></ul>
  • 18. startActivity, onActivityResult <ul><li>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. </li></ul><ul><li>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. </li></ul>
  • 19.  

×