跳到主要內容

【Android Development for Beginners】Lesson 2A : Hook Up Two Buttons

This Notes is according to the udacity free course :

Android Development for Beginners

Lesson 2A : Hook Up Two Buttons

Today's class is to modify our JustJava app.


  1. Add two buttons on the app.
  2. Check out the layout style. 
  3. Modify the xml for work.
  4. Check out the initial value.
    1. Quantity shows "2"
    2. Price shows "10"




xml code :

<?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" />
<!-- create a button called "+ -->
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="increment"
android:text="+"
android:layout_marginBottom="16dp"/>
<!-- make sure the initial value is 2-->
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="2"
android:textColor="@android:color/black"
android:textSize="16sp" />
<!--create a button called "-"-->
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="decrement"
android:text="-"
android:layout_marginBottom="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Price"
android:textAllCaps="true" />
<!-- make sure the initial price is $10-->
<TextView
android:id="@+id/price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="$10"
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>
java code :

if you want to add button's method, try to copy the "submitOrder" button.

package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends ActionBarActivity {
@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 quantity = 2;
display(quantity);
displayPrice(quantity * 5);
}
/**
* This method is called when the increment button is clicked.
*/
public void increment(View view) {
int quantity = 3;
display(quantity);
}
/**
* This method is called when the decrement button is clicked.
*/
public void decrement(View view) {
int quantity = 1;
display(quantity);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
/**
* This method displays the given price on the screen.
*/
private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}
}

留言

這個網誌中的熱門文章

【Quora 翻譯&整理】對於程式初學者嘗試解決問題的好網站推薦

原文出處: What are some suggested websites to solve programming problems for beginners? (2016/5/15) 此篇文章強調的是去參加project的那種網站推薦

第一章 電腦、網際網路與全球資訊網簡介

C++  How to Program 7/E 筆記 第一章 電腦、網際網路與全球資訊網簡介 學習目標 基本軟硬體觀念 物件技術觀念(類別、物件、屬性、行為、封裝、繼承) 程式語言的種類 典型的C++開發環境 業界標準物件導向素模語言——UML 沿革 網際網路、球球資訊網與Web 2.0現象的沿革 在Linux 的GNU C++ 與在Microsoft Visual C++環境中測試C++ 應用程式 *結構化程式設計(structured programming) *物件導向程式設計

【Quora 翻譯&筆記】你能從你的Android App 賺多少錢?

原文出處: How much money do you make from your Android application? 這方面的答案僅供參考: Stanley Idesis (2016/ 4/ 10) app : Quotograph 沒有內嵌廣告,採取免費使用的策略,另外有IAP(in app purchase)的模式,不過發現在play store 用戶的付費意願非常低。