studyjams 3B better android

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

1 target

本节目标将给出一个优化版本的order coffee.

知识点有:

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

2 demo show

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

about the android intent

谢谢网友的认真整理,在这里

END

发帖工具真的很好用.

期末大作业见.感谢studyjams提供这么好的平台!!

时间: 2025-01-02 18:24:24

studyjams 3B better android的相关文章

android学习的两三事- studyjams论坛学习有感

作者:66^3工作室 出处:https://code.csdn.net/titer1 联系:1307316一九六八(短信最佳) 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处. android学习的两三事 在整理这篇感想的时候,我正在星巴克,android sdk正在龟速的更新,最后的大作业还没有成行,于是就写写感想吧. 1 开篇 首先,我先自我介绍,一个典型的技术宅,叫我牛超人(csdn

Android内存那点事儿

好久没有写了,不是忘了,也不是懒,是因为迷茫了~~不知道该学什么,该写什么,该走什么样子的路,该做什么样子的人.我嘴笨,不知道怎么把自己会的讲给别人,我愿意分享,所以我就写出来,不管是对的,错的,希望大家能取其精华去其糟粕,不要因为我而误导诸位.废话不多说了~~ 你的应用内存泄漏了么? 要看是不是存在内存泄漏,首先我们要看到内存信息,如何看到内存信息呢?这里介绍一种方法,打开Eclipse连接手机,到DDMS中,选择要分析的应用,点击Update Heap也就是下图中1的图标,点击1图标之后会在

Android 透明度百分比对应的 十六进制

Android 透明度百分比对应的 十六进制 先把结果放在这里,方便大家查询,也方便自己,UI太喜欢用百分比表示了=.=! 透明度百分比对应的十六进制: (说明:百分比计算出来会有小数,按照常规的四舍五入处理,详情请往下查看) 百分比:0% HEX: 00 百分比:1% HEX: 30 百分比:2% HEX: 50 百分比:3% HEX: 80 百分比:4% HEX: A0 百分比:5% HEX: D0 百分比:6% HEX: F0 百分比:7% HEX: 12 百分比:8% HEX: 14 百

Android 签名比较

一. keytool -list -printcert -jarfile "%filename%" 二. 非常low方法:下载的应用安装能成功覆盖原应用则签名一致三. 作者:陈子腾链接:https://www.zhihu.com/question/20749413/answer/16715284来源:知乎著作权归作者所有,转载请联系作者获得授权. 1. 查找apk里的rsa文件 (Windows)> jar tf HelloWorld.apk |findstr RSA (Linu

android byte字节数组转换十六进制字符串(物联网开发总结)

想起前段时间的物联网的外包开发,经常遇到通过wifi接受的数据,要通过转换成十六进制字符串,或者最后又是十进制数据.都是根据双方的协议来开发的.那么我发送过去的数据也需要,经过特殊转换成byte字节发过去,硬件那边收到不至于乱码的数据. 1.硬件调试发给android这边是十六进制数据 原始数据:68 38 38 68 A 72 78 55 34 12 43 23 01 07 Y 00 00 00 0C 13 78 56 34 12 0C 3B 78 34 12 0C 26 78 56 34 1

Android 注入详解

Android下的注入的效果是类似于Windows下的dll注入,关于Windows下面的注入可以参考这篇文章Windows注入术.而Android一般处理器是arm架构,内核是基于linux,因此进程间是弱相互作用,不存在Windows下类似于CreateRemoteThread 作用的函数,可以在其他进程空间内创建线程来加载我们的.so文件,所以我们所采用的方法就是依赖于linux下的ptrace()函数,将目标进程作为我们进程的子进程操作目标进程的寄存器和内存来运行我们加载.so文件的代码

【转】Android kernel启动流程

;font-family:Arial, Console, Verdana, 'Courier New';line-height:normal;white-space:normal;background-color:#FFFFFF;"> linuxandroidmakefileimagecachealignment 虽然这里的Arm Linux kernel前面加上了Android,但实际上还是和普遍Arm linux kernel启动的过程一样的,这里只是结合一下Android的Makef

Android studio 签名使用转

来自http://www.cnblogs.com/xiwix/archive/2012/04/15/2447910.html 本文主要讲解Android应用程序签名相关的理论知识,包括:什么是签名.为什么要给应用程序签名.如何给应用程序签名等. 1.什么是签名?      如果这个问题不是放在Android开发中来问,如果是放在一个普通的版块,我想大家都知道签名的含义.可往往就是将一些生活中常用的术语放在计算机这种专业领域,大家就开始迷惑了.计算机所做的事情,或者说编程语言所做的事情,不正是在尽

android签名机制

http://blog.csdn.net/feiyangxiaomi/article/details/40298155 1.android为什么要签名 所有的Android应用程序都要求开发人员用一个证书进行数字签名,anroid系统不会安装没有进行签名的由于程序.平时我们的程序可以在模拟器上安装并运行,是因为在应用程序开发期间,由于是以Debug面试进行编译的,因此ADT根据会自动用默认的密钥和证书来进行签名,而在以发布模式编译时,apk文件就不会得到自动签名,这样就需要进行手工签名.   给