布局层次草图
String
字符串常量,与int一样
数据类型
+ 变量名
= 变量值
;
String priceMassage = "Free";
需要注意的是,String值由两个双引号包围着
如果需要使用双引号,则要用转义字符\"
将双引号转义,如
String priceMassage = "She said \"1 dollar\"";
\n
是换行的转义字符
字符串的连接
使用 +
来连接,如
String priceMassage = "Amount Due" + "¥10";
如果将字符串和整形数字连接起来会生成一个新的字符串,如
String priceMassage = "¥" + 100;
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="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-"
android:layout_marginRight="8dp"
android:onClick="decrement"/>
<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="8dp"
android:text="+"
android:onClick="increment"/>
</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="¥0.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) {
int price = quantity * 5;
String priceMassage = "Total: ¥" + price;
priceMassage = priceMassage + "\nThank you!";
displayMassage(priceMassage);
}
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));
}
private void displayMassage(String massage) {
TextView priceTextView = (TextView)findViewById(R.id.price_text_view);
priceTextView.setText(massage);
}
}