Your SlideShare is downloading. ×
0
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
講義
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

講義

323

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
323
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
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. Rung-Hung Gau Department of Computer Science and Engineering National Sun Yat-Sen University Kaohsiung, Taiwan A Basic Application
  • 2. Outline <ul><li>Android Projects </li></ul><ul><li>Activities and Views </li></ul><ul><li>Bundle </li></ul><ul><li>Dissecting the Activity </li></ul><ul><li>Building and Running the Activity </li></ul>
  • 3. Create an Android Project <ul><li>To work with anything in Android, you need a project. </li></ul><ul><li>With ordinary Java, you could just write a program as a single file, compile it with javac, and run it. </li></ul><ul><li>Android is more complex, but to help keep it manageable, Google has supplied tools to help create the project. </li></ul><ul><li>If you are using an Android-enabled IDE, such as Eclipse with the Android plugin, you can create a project inside of the IDE (e.g., select File &gt; New &gt; Project, then choose Android &gt; Android Project). </li></ul>
  • 4. Create an Android Project <ul><li>Your project&apos;s src/ directory contains the standard Java-style tree of directories based upon the Java package you chose when you created the project (e.g., edu.nsysu.android results in src/edu/nsysu/android/). </li></ul><ul><li>Inside the innermost directory you should find a pre-generated source file named Now.java, which is where your first activity will go. </li></ul><ul><li>Open Now.java in your editor and paste in the following code: </li></ul>
  • 5. <ul><li>package edu.nsysu.android.skeleton; </li></ul><ul><li>import android.app.Activity; </li></ul><ul><li>import android.os.Bundle; </li></ul><ul><li>import android.view.View; </li></ul><ul><li>import android.widget.Button; </li></ul><ul><li>import java.util.Date; </li></ul><ul><li>public class Now extends Activity implements View.OnClickListener { </li></ul><ul><li>Button btn; </li></ul><ul><li>@Override </li></ul><ul><li>public void onCreate(Bundle icicle) { </li></ul><ul><li>super. onCreate(icicle); </li></ul><ul><li>btn = new Button(this); </li></ul><ul><li>btn. setOnClickListener(this); </li></ul>
  • 6. <ul><li>updateTime(); </li></ul><ul><li>setContentView(btn); </li></ul><ul><li>} </li></ul><ul><li>public void onClick(View view) { </li></ul><ul><li>updateTime(); </li></ul><ul><li>} </li></ul><ul><li>private void updateTime() { </li></ul><ul><li>btn. setText(new Date().toString()); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  • 7. Dissecting the Activity <ul><li>The package declaration needs to be the same as the one you used when creating the project. </li></ul><ul><li>And, like any other Java project, you need to import any classes you reference. </li></ul><ul><li>Most of the Android-specific classes are in the android package. </li></ul>
  • 8. Activity and Event Listener <ul><li>Remember that not every Java SE class is available to Android programs! </li></ul><ul><li>Activities are public classes, inheriting from the android.Activity base class. </li></ul><ul><li>In this case, the activity holds a button (btn). Since, for simplicity, we want to trap all button clicks just within the activity itself, we also have the activity class implement OnClickListener. </li></ul>
  • 9. Activities and Views <ul><li>Activities are your application’s presentation layer. </li></ul><ul><li>Every screen in your application will be an extension of the Activity class. </li></ul><ul><li>All visual components in Android descend from the View class and are referred generically as Views. </li></ul><ul><li>Activities use Views to form graphical user interface. </li></ul><ul><li>In terms of desktop development, an Activity is equivalent to a Form. </li></ul>
  • 10. Activities and Views <ul><li>A new Activity starts with an empty screen onto which you place your User Interface. </li></ul><ul><li>To set the User Interface, call setContentView() , passing in the View instance (typically a layout) to display. </li></ul><ul><ul><li>View-&gt;ViewGroup-&gt;Layout </li></ul></ul><ul><ul><li>View-&gt;ViewGroup-&gt;Widget (Compound Control) </li></ul></ul><ul><ul><li>View-&gt;Control </li></ul></ul><ul><li>You almost always assign an Activity’s User Interface when overriding its onCreate handler. </li></ul>
  • 11. Lifecycle Management and Bundle <ul><li>onCreate() is a lifecycle management method. </li></ul><ul><li>Saving instance state is handled by onSaveInstanceState(). </li></ul><ul><li>This supplies a Bundle, into which activities can pour whatever data they need (e.g., the number showing on the calculator&apos;s display). </li></ul>
  • 12. Lifecycle Management and Bundle <ul><li>That instance state is provided to you again in two laces: </li></ul><ul><ul><li>onCreate() </li></ul></ul><ul><ul><li>onRestoreInstanceState() </li></ul></ul><ul><li>More on Android lifecycle management later </li></ul>
  • 13. Dissecting the Program <ul><li>The onCreate() method is invoked when the activity is started. </li></ul><ul><li>The first thing you should do is chain upward to the superclass, so the stock Android activity initialization can be done. </li></ul><ul><li>In our implementation, we then create the button instance (new Button(this)), tell it to send all button clicks to the activity instance itself (via setOnClickListener()). </li></ul>
  • 14. Dissecting the Program <ul><li>We set the activity&apos;s content view to be the button itself (via setContentView()). </li></ul><ul><li>In Android, a button click causes onClick() to be invoked in the OnClickListener instance configured for the button. </li></ul><ul><li>The listener is provided the view that triggered the click (in this case, the button). </li></ul>
  • 15. Dissecting the Program <ul><li>In this program, when we open the activity (onCreate()) or when the button is clicked (onClick()), we update the button&apos;s label to be the current time via setText. </li></ul>
  • 16. Building and Running the Activity <ul><li>To build the activity, either use your IDE&apos;s built-in Android packaging tool, or run ant in the base directory of your project. </li></ul><ul><li>Then, to run the activity: </li></ul><ul><ul><li>Launch the emulator (e.g., run tools/emulator from your Android SDK installation) </li></ul></ul>
  • 17.
  • 18. Building and Running the Activity <ul><ul><li>Install the package (e.g., run tools/adb install /path/to/this/example/bin/Now.apk from your Android SDK installation) </li></ul></ul><ul><ul><li>View the list of installed applications in the emulator and find the &amp;quot;Now&amp;quot; application </li></ul></ul>
  • 19.
  • 20. Building and Running the Activity <ul><ul><li>Open that application </li></ul></ul><ul><li>You should see an activity screen akin to the next page: </li></ul><ul><li>Clicking the button – in other words, pretty much anywhere on the phone&apos;s screen – will update the time shown in the button&apos;s label. </li></ul>

×