StudyJams-第08课_多语言支持

1 logcat:
  1.1 Log.v(tag, message);//Log.d、Log.i、Log.w、Log.e
2 垂直的线性布局过长,无法显示全部数据。使其可以滑动。
  2.1 使用ScrollView
3 新控件学习
  3.1 CheackBox
    3.1.1 cheakBox.isChecked()
  3.2 EditText
    3.2.1 editText.getText().toString()
4 Intent
  4.1 强大的东东,Activity和Activity之间的纽带啊!!!
  4.2 还可以携带数据,你怕不怕?
5 多语言支持
  5.1 中文(中國):values-zh-rCN
6 style的用法
  6.1 一处编写,多处调用
  6.2 一处修改,多处改变
7 标签的使用
8 show code:
  8.1 MainActivity.java:

package wang.relish.ordercoffee;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


    AutoCompleteTextView etName;
    TextView tvQuantity;
    TextView tvPrice;
    CheckBox cbCream;
    CheckBox cbChocolate;
    int unitPrice = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etName = (AutoCompleteTextView) findViewById(R.id.etName);
        tvQuantity = (TextView) findViewById(R.id.tvQuantity);
        tvPrice = (TextView) findViewById(R.id.tvOrderSummary);
        cbCream = (CheckBox) findViewById(R.id.cbCream);
        cbChocolate = (CheckBox) findViewById(R.id.cbChocolate);
    }

    public void increase(View v) {
        String currentQuantityStr = tvQuantity.getText().toString();
        int currentQuantity = Integer.parseInt(currentQuantityStr);
        tvQuantity.setText(String.valueOf(currentQuantity + 1));
    }

    public void decrease(View v) {
        String currentQuantityStr = tvQuantity.getText().toString();
        int currentQuantity = Integer.parseInt(currentQuantityStr);
        if (currentQuantity == 0) {
            return;
        }
        tvQuantity.setText(String.valueOf("$" + (currentQuantity - 1)));
    }

    public void submitOrder(View v) {
        String name = etName.getText().toString();
        boolean addCream = cbCream.isChecked();
        boolean addChocolate = cbChocolate.isChecked();
        int quantity = Integer.parseInt(tvQuantity.getText().toString());
        int total = quantity * unitPrice + (addCream ? 1 : 0) + (addChocolate ? 2 : 0);
        String message = makeupOrderSummary(name, addCream, addChocolate, quantity, total);
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:"));
        intent.putExtra(Intent.EXTRA_SUBJECT, "Just Java order for " + name);
        intent.putExtra(Intent.EXTRA_TEXT, message);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
        tvPrice.setText(message);
    }

    private String makeupOrderSummary(String name, boolean addCream, boolean addChocolate, int quantity, int total) {
        String s = getResources().getString(R.string.order_summary_name, name);
        s += "\n" + getResources().getString(R.string.order_summary_whipped_cream, addCream+"");
        s += "\n"+getResources().getString(R.string.order_summary_chocolate, addChocolate+"");
        s += "\n"+getResources().getString(R.string.order_summary_quantity,quantity);
        s += "\n"+getResources().getString(R.string.order_summary_price,total);
        s += "\n"+getResources().getString(R.string.thank_you);
        return s;
    }
}

  8.2 activity_main.xml


<ScrollView 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"
    tools:context="wang.relish.ordercoffee.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <AutoCompleteTextView
            android:id="@+id/etName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:hint="@string/name"
            android:inputType="textCapWords" />

        <TextView
            style="@style/HeaderTextStyle"
            android:text="@string/toppings" />

        <CheckBox
            android:id="@+id/cbCream"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:text="@string/whipped_cream" />

        <CheckBox
            android:id="@+id/cbChocolate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="16dp"
            android:text="@string/chocolate" />

        <TextView
            style="@style/HeaderTextStyle"
            android:text="@string/quantity" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:orientation="horizontal">

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:onClick="decrease"
                android:text="-" />

            <TextView
                android:id="@+id/tvQuantity"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginBottom="16dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:gravity="center_vertical"
                android:text="@string/initial_quantity_value"
                android:textSize="24sp" />

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:onClick="increase"
                android:text="+" />
        LinearLayout>

        <TextView
            style="@style/HeaderTextStyle"
            android:text="@string/order_summary" />

        <TextView
            android:id="@+id/tvOrderSummary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:text="$0" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:onClick="submitOrder"
            android:text="@string/order" />
    LinearLayout>
ScrollView>

  8.3 strings.xml

"http://schemas.android.com/tools">
    <string name="app_name">Just Javastring>
    <string name="name">Namestring>
    <string name="toppings">Toppingsstring>
    <string name="whipped_cream">Whipped creamstring>
    <string name="chocolate">Chocolatestring>
    <string name="quantity">Quantitystring>
    <string name="initial_quantity_value">2string>
    <string name="order">Orderstring>
    <string name="order_summary_name">Name:"Amy" id="name">%sstring>
    <string name="order_summary_whipped_cream">Add whipped cream? "true" id="addWhippedCream">%sstring>
    <string name="order_summary_chocolate">Add chocolate? "true" id="addWhippedCream">%sstring>
    <string name="order_summary_quantity">Quantity: "2" id="quantity">%sstring>
    <string name="order_summary_price">Total: "$10" id="price">%dstring>
    <string name="thank_you">Thank you!string>
    <string name="order_summary_email_subject">Just Java order for "Amy" id="name">%sstring>
    <string name="order_summary">Order Summarystring>

复制代码
values-zh-rCN/strings.xml
"http://schemas.android.com/tools">
    <string name="app_name">点咖啡string>
    <string name="name">姓名string>
    <string name="toppings">附加string>
    <string name="whipped_cream">奶油string>
    <string name="chocolate">巧克力string>
    <string name="quantity">数量string>
    <string name="initial_quantity_value">2string>
    <string name="order">下单string>
    <string name="order_summary_name">姓名:"艾米" id="name">%sstring>
    <string name="order_summary_whipped_cream">加奶油吗? "加" id="addWhippedCream">%sstring>
    <string name="order_summary_chocolate">加巧克力吗? "加" id="addWhippedCream">%sstring>
    <string name="order_summary_quantity">数量: "2" id="quantity">%dstring>
    <string name="order_summary_price">总计: "$10" id="price">%dstring>
    <string name="thank_you">谢谢品尝!string>
    <string name="order_summary_email_subject">"Amy" id="name">%s的点咖啡订单string>
    <string name="order_summary">订单总计string>

  8.4 styles.xml



    -- Base application theme. -->
    

    

    


9 show 截屏:
  9.1 中文界面:
  StudyJams-第08课_多语言支持_第1张图片
  9.2 语言切换:
  StudyJams-第08课_多语言支持_第2张图片
  9.3 英文界面:
  StudyJams-第08课_多语言支持_第3张图片

你可能感兴趣的:(【StudyJamsS01】)