studyjams 3B better android

这是其中最后一次作业,备份到此。

1 target

本节目标将给出一个优化版本的order coffee.
知识点有:

  • 使用intent联通其他app
  • 利用style.xml 可以复用style
  • 利用string.xml 分离语言和逻辑
  • 更多调试android的方法,比如log + toast
  • 使用scrollview
  • 更多的控件,比如checkbox

2 demo show

studyjams 3B better android_第1张图片

3 demo code

main_activity.java

package com.example.android.justjava;

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

import java.text.NumberFormat;

/** * This app displays an order form to order coffee. */
public class MainActivity extends AppCompatActivity {

    int quantity = 2;

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

    /** * This method is called when the plus button is clicked. */
    public void increment(View view) {
        if (quantity == 100) {
            return;
        }
        quantity = quantity + 1;
        displayQuantity(quantity);
    }

    /** * This method is called when the minus button is clicked. */
    public void decrement(View view) {
        if (quantity == 0) {
            return;
        }
        quantity = quantity - 1;
        displayQuantity(quantity);
    }

    /** * This method is called when the order button is clicked. */
    public void submitOrder(View view) {
        // Get user's name
        EditText nameField = (EditText) findViewById(R.id.name_field);
        Editable nameEditable = nameField.getText();
        String name = nameEditable.toString();

        // Figure out if the user wants whipped cream topping
        CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
        boolean hasWhippedCream = whippedCreamCheckBox.isChecked();

        // Figure out if the user wants whipped cream topping
        CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
        boolean hasChocolate = chocolateCheckBox.isChecked();

        // Calculate the price
        int price = calculatePrice(hasWhippedCream, hasChocolate);

        // Display the order summary on the screen
        String message = createOrderSummary(name, price, hasWhippedCream, hasChocolate);


        TextView resTextView = (TextView) findViewById(
                R.id.result);
        resTextView.setText(message);


        // Use an intent to launch an email app.
        // Send the order summary in the email body.

        //鉴于国内的环境,note2没有触发出email邮件app
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:")); // only email apps should handle this
        intent.putExtra(Intent.EXTRA_SUBJECT,
                getString(R.string.order_summary_email_subject, name));
        intent.putExtra(Intent.EXTRA_TEXT, message);

        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }

    }

    /** * Calculates the price of the order. * * @param addWhippedCream is whether or not we should include whipped cream topping in the price * @param addChocolate is whether or not we should include whipped cream topping in the price * @return total price */
    private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
        // First calculate the price of one cup of coffee
        int basePrice = 5;

        // If the user wants whipped cream, add $1 per cup
        if (addWhippedCream) {
            basePrice = basePrice + 1;
        }

        // If the user wants chocolate, add $2 per cup
        if (addChocolate) {
            basePrice = basePrice + 2;
        }

        // Calculate the total order price by multiplying by the quantity
        return quantity * basePrice;
    }

    /** * Create summary of the order. * * @param name on the order * @param price of the order * @param addWhippedCream is whether or not to add whipped cream to the coffee * @param addChocolate is whether or not to add chocolate to the coffee * @return text summary */
    private String createOrderSummary(String name, int price, boolean addWhippedCream,
                                      boolean addChocolate) {


        String priceMessage = getString(R.string.order_summary_name, name);
        priceMessage += "\n" + getString(R.string.order_summary_whipped_cream, addWhippedCream);
        priceMessage += "\n" + getString(R.string.order_summary_chocolate, addChocolate);
        priceMessage += "\n" + getString(R.string.order_summary_quantity, quantity);
        priceMessage += "\n" + getString(R.string.order_summary_price,
                NumberFormat.getCurrencyInstance().format(price));
        priceMessage += "\n" + getString(R.string.thank_you);



        return priceMessage;
    }

    /** * This method displays the given quantity value on the screen. */
    private void displayQuantity(int numberOfCoffees) {
        TextView quantityTextView = (TextView) findViewById(
                R.id.quantity_text_view);
        quantityTextView.setText("" + numberOfCoffees);
    }
}

layout.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=".MainActivity">

    <LinearLayout  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">

        <EditText  android:id="@+id/name_field" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/name" android:inputType="text" />

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

        <CheckBox  android:id="@+id/whipped_cream_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="24dp" android:text="@string/whipped_cream" android:textSize="16sp" />

        <CheckBox  android:id="@+id/chocolate_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="24dp" android:text="@string/chocolate" android:textSize="16sp" />

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

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

            <Button  android:layout_width="48dp" android:layout_height="48dp" android:onClick="decrement" android:text="-" />

            <TextView  android:id="@+id/quantity_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="8dp" android:paddingRight="8dp" android:text="@string/initial_quantity_value" android:textColor="@android:color/black" android:textSize="16sp" />

            <Button  android:layout_width="48dp" android:layout_height="48dp" android:onClick="increment" android:text="+" />

        </LinearLayout>

        <Button  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:onClick="submitOrder" android:text="@string/order" />
        <TextView  android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="8dp" android:paddingRight="8dp" android:text="@string/result" android:textColor="@android:color/black" android:textSize="16sp" />

    </LinearLayout>
</ScrollView>

style.xml


<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- App branding color for the app bar --> <item name="colorPrimary">#009688</item> <!-- Darker variant for the status bar and contextual app bars --> <item name="colorPrimaryDark">#00796B</item> <!-- Theme UI controls like checkboxes and text fields --> <item name="colorAccent">#536DFE</item> </style>

    <!-- Style for header text in the order form -->
    <style name="HeaderTextStyle"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">48dp</item> <item name="android:gravity">center_vertical</item> <item name="android:textAllCaps">true</item> <item name="android:textSize">15sp</item> </style>

</resources>

string.xml

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <!-- Title for the application. [CHAR LIMIT=12] -->
     <string name="app_name">studyjams Coffee</string>

     <!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
     <string name="name">Name</string>

     <!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
     <string name="toppings">Toppings</string>

     <!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
     <string name="whipped_cream">Whipped cream</string>

     <!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
     <string name="chocolate">Chocolate</string>

     <!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
     <string name="quantity">Quantity</string>

     <!-- Hint text display in the empty field for the user's name [CHAR LIMIT=5] -->
     <string name="initial_quantity_value">2</string>

     <!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
     <string name="order">Order</string>

     <!-- Name for the order summary. It will be shown in the format of "Name: Amy" where Amy is the user's name. [CHAR LIMIT=NONE] -->
     <string name="order_summary_name">Name: <xliff:g id="name" example="Amy">%s</xliff:g></string>

     <!-- Whipped cream topping for the order summary. It will be shown in the format of "Add whipped cream? true" or "Add whipped cream? false". [CHAR LIMIT=NONE] -->
     <string name="order_summary_whipped_cream">Add whipped cream? <xliff:g id="addWhippedCream" example="true">%b</xliff:g></string>

     <!-- Chocolate topping for the order summary. It will be shown in the format of "Add chocolate? true" or "Add chocolate? false". [CHAR LIMIT=NONE] -->
     <string name="order_summary_chocolate">Add chocolate? <xliff:g id="addChocolate" example="true">%b</xliff:g></string>

     <!-- Quantity of coffee cups for the order summary. It will be shown in the format of "Quantity: 2", where 2 is the number of cups ordered. [CHAR LIMIT=NONE] -->
     <string name="order_summary_quantity">Quantity: <xliff:g id="quantity" example="2">%d</xliff:g></string>

    <string name="result">Report: <xliff:g id="result" example="">%s</xliff:g></string>

     <!-- Total price for the order summary. It will be shown in the format of "Total: $10" where $10 is the price. [CHAR LIMIT=NONE] -->
     <string name="order_summary_price">Total: <xliff:g id="price" example="$10">%s</xliff:g></string>

     <!-- Thank you message for the order summary. [CHAR LIMIT=NONE] -->
     <string name="thank_you">Thank you! 君莫要贪杯!</string>

     <!-- Subject line for the order summary email. It will be in the format of "Just Java order for Amy" where Amy is the user's name. [CHAR LIMIT=NONE] -->
     <string name="order_summary_email_subject">Just Java order for <xliff:g id="name" example="Amy">%s</xliff:g></string>
</resources>

4 notes

about the structure

studyjams 3B better android_第2张图片

about the android intent

谢谢网友的认真整理,在这里
studyjams 3B better android_第3张图片

END

发帖工具真的很好用.
期末大作业见.感谢studyjams提供这么好的平台!!

你可能感兴趣的:(android,studyjams)