Part 3 of 3
Transcription
Part 3 of 3
CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing Lab & Demo 2 (3 of 3) Hello-Goodbye App Tutorial Reference Android Programming Concepts, by Trish Cornez and Richard Cornez, pubslihed by Jones & Barlett Learning, pp. 22-67. Android Developer API Guides, http://developer.android.com/guide/index.html Paul I-Hai Lin, Professor Spring 2016 A Specialty Course Purdue University M.S. Technology Graduate Program Dept. of Computer, Electrical and Information Technology Purdue University Fort Wayne Campus Prof. Paul Lin 1 Hello-Goodbye App Coding the Activity for the “Hello-Goodbye” app. Open MainActivity file located in java.com.yourName.hellogoodbye Prof. Paul Lin 2 1 Hello-Goodbye App Coding the Activity for the “Hello-Goodbye” app. Open MainActivity file located in java.com.yourName.hellogoodbye Prof. Paul Lin 3 Hello-Goodbye App Edit the Java code as shown below (see pp. 54-55 – some errors??) Prof. Paul Lin 4 2 Hello-Goodbye App Edit the Java code as shown below (see pp. 54-55 – some errors??) Prof. Paul Lin 5 Hello-Goodbye App Edit the Java code as shown below (see pp. 54-55 – some errors??) Prof. Paul Lin 6 3 Hello-Goodbye App The revised Java code as shown below (see pp. 54-55 – some errors??) package com.example.lin.hellogoodbye; import android.app.Activity; import android.widget.Button; import android.widget.TextView; import android.os.Bundle; import android.view.View; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { // Declare text reference to the Interface Layout Component private TextView greetingTextView; // Indicate Hello is currently Displayed private boolean isHello; Prof. Paul Lin 7 Hello-Goodbye App The revised Java code as shown below - continue @Override protected void onCreate(Bundle savedInstanceState) { //TASK 1: Inflate the main screen layout used by the app super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //TASK 2: Establish references to the TextView and Button greetingTextView = (TextView) findViewById(R.id.textView); //TASK 3: Initialze Greetings initializeGreeting(); //TASK 4: Register the Listner Event for the Button Button exclaim_btn = (Button) findViewById(R.id.button); exclaim_btn.setOnClickListener(toggleGreeting); } Prof. Paul Lin 8 4 Hello-Goodbye App The revised Java code as shown below - continue private final View.OnClickListener toggleGreeting = new View.OnClickListener(){ public void onClick(View btn){ //Task: Construct the toggle greeting if(isHello) { isHello = false; greetingTextView.setText(R.string.goodbye); } else{ isHello = true; greetingTextView.setText(R.string.hello); } } }; Prof. Paul Lin 9 Hello-Goodbye App The revised Java code as shown below - continue private void initializeGreeting(){ isHello = true; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } Prof. Paul Lin 10 5 Hello-Goodbye App The revised Java code as shown below - continue @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } Prof. Paul Lin 11 Hello-Goodbye App Click the green triangle to run the app using Emulator. Prof. Paul Lin 12 6 Hello-Goodbye App Click the green triangle to run the app using Emulator. Click EXCLAIMATION button to say “Goodbte” Prof. Paul Lin 13 Hello-Goodbye App The Smartphone Screen Prof. Paul Lin 14 7 Summary Q/A? Prof. Paul Lin 15 8
Similar documents
YÉÇàá - Cleveland State University
android:layout_height="wrap_content"
d id l
t h i ht "
t t"
android:orientation="horizontal" >