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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.husky.justjava.MainActivity">
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QUANTITY!"
android:textColor="@android:color/darker_gray"
android:textSize="25sp"
android:layout_marginBottom="16dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/decrease"
android:text="+"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:onClick="increment"/>
<TextView
android:id="@+id/quantity_text_view"
android:text="0"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/decrease"
android:textSize="25sp"/>
<Button
android:id="@+id/plus"
android:text="-"
android:layout_toRightOf="@+id/quantity_text_view"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="decrement"/>
</RelativeLayout>
<TextView
android:id="@+id/price"
android:text="PRICE"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp" />
<TextView
android:id="@+id/price_text_view"
android:text="$0"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="ORDER"
android:onClick="submitOrder"/>
</LinearLayout>
MainActivity.java
package com.husky.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 AppCompatActivity {
@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) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
int numberOfCoffee = Integer.parseInt(quantityTextView.getText().toString());
// display(numberOfCoffee);
// displayPrice(numberOfCoffee * 5);
String priceMessage = "$ "+(numberOfCoffee*5)+"\n"+"Thank you!";
displayMessage(priceMessage);
}
public void increment(View view){
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
int quantity = Integer.parseInt(quantityTextView.getText().toString())+1;
display(quantity);
}
public void decrement(View view){
TextView quantityTextView = (TextView)findViewById(R.id.quantity_text_view);
int quantity = Integer.parseInt(quantityTextView.getText().toString())-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);
}
private void displayPrice(int number){
TextView priceTextView= (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}
/**
* This method displays the given text on the screen.
*/
private void displayMessage(String message) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(message);
}
}
重点内容
- Nested ViewGroups嵌套的视图组
可以在LinearLayout中嵌套RelativeLayout
2.按钮的onClick方法android:onClick=”submitOrder”,当然在MainActivity.java中要写这个方法,public void submitOrder(View view){} - TextView quantityTextView = (TextView)findViewById(R.id.quantity_text_view);
int quantity = Integer.parseInt(quantityTextView.getText().toString())-1; - quantityTextView.setText(“” + number);这个很重要,number是int类型,必须要转型,不然会一直报错,”” + number,或者scoreView.setText(String.valueOf(score));
- priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));只是将数字前面转型成字符,譬如10数字,转型成¥10
- 6.
题外话,如果要学习android,很推荐https://cn.udacity.com/
优达学城