This Notes is according to the udacity free course :
Android Development for Beginners
Lesson 3A : Object Oriented Programming
Output from a method
If you want to output from a method : return value is the key point.
Review



A method has a return data type
and there will need the return message
to return the value to the upper layer-----UI
The word write behind the return data can't be reachable because the declaration were done in "return message".
If there is no need to use return value, the method return data type is void.
Use Return Value

Define and Call a Method

The displayMessage Method - Quiz
First Step

Second Step

Android Development for Beginners
Lesson 3A : Object Oriented Programming
Output from a method
If you want to output from a method : return value is the key point.
Review



A method has a return data type
and there will need the return message
to return the value to the upper layer-----UI
The word write behind the return data can't be reachable because the declaration were done in "return message".
If there is no need to use return value, the method return data type is void.
Use Return Value

Define and Call a Method

The displayMessage Method - Quiz
First Step

Second Step

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.justjava; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.view.View; | |
import android.widget.TextView; | |
/** | |
* This app displays an order form to order coffee. | |
*/ | |
public class MainActivity extends ActionBarActivity { | |
int quantity = 1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
/** | |
* This method is called when the order button is clicked. | |
*/ | |
public void submitOrder(View view) { | |
int price = calculatePrice(); | |
displayMessage(createOrderSummary()); | |
} | |
/** | |
* Calculates the price of the order. | |
* | |
* @return total price | |
*/ | |
private int calculatePrice() { | |
int price = quantity * 5; | |
return price; | |
} | |
/** | |
* Create summary of the order. | |
* | |
* | |
* @return text "order" | |
*/ | |
private String createOrderSummary(){ | |
int price = quantity * 5; | |
String order = "Name: Kaptain Kunal" + "\nQuantity: " + quantity + "\nTotal: " + price + "\nthank you"; | |
return order; | |
} | |
/** | |
* This method is called when the increment button is clicked. | |
*/ | |
public void increment(View view) { | |
quantity = quantity + 1; | |
displayQuantity(quantity); | |
} | |
/** | |
* This method is called when the decrement button is clicked. | |
*/ | |
public void decrement(View view) { | |
quantity = quantity - 1; | |
displayQuantity(quantity); | |
} | |
/** | |
* This method displays the given quantity value on the screen. | |
*/ | |
private void displayQuantity(int number) { | |
TextView quantityTextView = (TextView) findViewById( | |
R.id.quantity_text_view); | |
quantityTextView.setText("" + number); | |
} | |
/** | |
* This method displays the given text on the screen. | |
*/ | |
private void displayMessage(String message) { | |
TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view); | |
orderSummaryTextView.setText(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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:orientation="vertical" | |
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="com.example.android.justjava.MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="16dp" | |
android:text="Quantity" | |
android:textAllCaps="true" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:layout_marginBottom="16dp"> | |
<Button | |
android:layout_width="48dp" | |
android:layout_height="48dp" | |
android:onClick="decrement" | |
android:text="-" /> | |
<TextView | |
android:id="@+id/quantity_text_view" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:paddingLeft="8dp" | |
android:paddingRight="8dp" | |
android:text="1" | |
android:textColor="@android:color/black" | |
android:textSize="16sp" /> | |
<Button | |
android:layout_width="48dp" | |
android:layout_height="48dp" | |
android:onClick="increment" | |
android:text="+" /> | |
</LinearLayout> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="16dp" | |
android:text="Order Summary" | |
android:textAllCaps="true" /> | |
<TextView | |
android:id="@+id/order_summary_text_view" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="16dp" | |
android:text="$5 " | |
android:textColor="@android:color/black" | |
android:textSize="16sp" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:onClick="submitOrder" | |
android:text="order" /> | |
</LinearLayout> |
留言
張貼留言