訂咖啡App
在Android Studio 中 可以通過ctrl + alt + L
格式化代碼格式
認(rèn)識(shí)活動(dòng)——activity
顯示視圖與交互的界面,是個(gè)java文件
Button
onClick屬性
android:onClick="submitOrder"http://當(dāng)按下按鈕時(shí)會(huì)去調(diào)用submitOrder方法
<Button//
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Order"
android:textAllCaps="true"
android:onClick="submitOrder"/>
public void submitOrder(View view) {
int numberOfCoffees = 2;
display(numberOfCoffees);
displayPrice(numberOfCoffees * 5);
}
聲明變量
【變量類型】 +【變量名】= 【變量值】;
int 整形數(shù)據(jù)
如 int numberOfCoffees = 2;
變量作用域
如果一個(gè)變量是在方法中被聲明的案训,它只能在這個(gè)方法的作用域內(nèi)被使用
Debug
- 運(yùn)用DDMS查看錯(cuò)誤日志
- 添加斷點(diǎn)秦忿,然后用debug模式
activity_main.xml
<?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.clixin.justjava.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Quantity"
android:textAllCaps="true"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#344567" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+"
android:layout_marginRight="16dp"
android:onClick="increment"/>
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textAppearance="?android:textAppearanceLarge" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:text="-"
android:onClick="decrement"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="Price"
android:textAllCaps="true"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#344567" />
<TextView
android:id="@+id/price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥10.0"
android:textAppearance="?android:textAppearanceLarge" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Order"
android:textAllCaps="true"
android:textAppearance="?android:textAppearanceLarge"
android:onClick="submitOrder"/>
</LinearLayout>
MainActivity.java
package com.example.clixin.justjava;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
public class MainActivity extends ActionBarActivity {
int quantity = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void submitOrder(View view) {
display(quantity);
displayPrice(quantity * 5);
}
public void increment(View view) {
quantity = quantity + 1;
display(quantity);
}
public void decrement(View view) {
quantity = quantity - 1;
display(quantity);
}
private void display (int number) {
TextView quantityTextView = (TextView)findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
private void displayPrice(int number) {
TextView priceTextView = (TextView)findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}
}