SJ64 认识字符串,完善JUST JAVA

布局层次草图

SJ64 认识字符串,完善JUST JAVA_第1张图片

String

字符串常量,与int一样

数据类型 + 变量名 = 变量值;

String priceMassage = "Free";

需要注意的是,String值由两个双引号包围着

如果需要使用双引号,则要用转义字符\" 将双引号转义,如

String priceMassage = "She said \"1 dollar\"";

\n 是换行的转义字符

字符串的连接

使用 + 来连接,如

String priceMassage = "Amount Due" + "¥10";

如果将字符串和整形数字连接起来会生成一个新的字符串,如
String priceMassage = "¥" + 100;

SJ64 认识字符串,完善JUST JAVA_第2张图片
JUST JAVA
SJ64 认识字符串,完善JUST JAVA_第3张图片
JUST JAVA

activity_main.xml







    

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);
    }
}

你可能感兴趣的:(SJ64 认识字符串,完善JUST JAVA)