SJ64 資源調(diào)用辆布,完善JUST JAVA

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));//調(diào)用郵箱App

完成Just Java ——源碼如下

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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">

        <EditText
            android:id="@+id/name_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/type_your_name_here"/>

        <TextView
            style="@style/CustomText"
            android:layout_marginBottom="16dp"
            android:text="@string/toppings"
             />

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

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

        <TextView
            style="@style/CustomText"
            android:layout_marginBottom="16dp"
            android:layout_marginTop="16dp"
            android:text="@string/quantity"
            />

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

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginRight="8dp"
                android:onClick="decrement"
                android:text="@string/decrement" />

            <TextView
                android:id="@+id/quantity_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/quantity_hint"
                android:textAppearance="?android:textAppearanceLarge" />

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginLeft="8dp"
                android:onClick="increment"
                android:text="@string/increment" />

        </LinearLayout>


        <TextView
            style="@style/CustomText"
            android:layout_marginBottom="16dp"
            android:layout_marginTop="16dp"
            android:text="@string/order_summary"
             />

        <Button
            style="@style/CustomText"
            android:layout_marginTop="16dp"
            android:onClick="submitOrder"
            android:text="@string/order"
             />

    </LinearLayout>
</ScrollView>

Style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    
    <style name="CustomText" >
        <item name="android:textAppearance">?android:textAppearanceLarge</item>
        <item name="android:textAllCaps">true</item>
        <item name="android:textColor">#344567</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    
    </style>

</resources>

String.xml

<resources>
    <string name="app_name">Just Java</string>
    <string name="type_your_name_here">Type your name here</string>
    <string name="toppings">Toppings</string>
    <string name="whippied_cream">Whippied Cream / 1¥</string>
    <string name="chocolate">chocolate / 2¥</string>
    <string name="quantity">Quantity</string>
    <string name="decrement">-</string>
    <string name="quantity_hint">1</string>
    <string name="increment">+</string>
    <string name="order_summary">Order Summary</string>
    <string name="price_hint">¥0</string>
    <string name="order">Order</string>
    <string name="order_summary_name">Name:</string>
    <string name="order_summary_whippied_cream">Has Whippied Cream?</string>
    <string name="order_summary_chocolate">Has Chocolate?</string>
    <string name="order_summary_total">total_price:</string>
    <string name="thank_you">Thank you!</string>
    <string name="order_summary_quantity">Quantity:</string>
    <string name="not_less_than_1">you can order at least 1 coffee</string>
    <string name="not_more_than_20">you can order at most 20 coffees</string>
    <string name="just_order_for">Just order for</string>
</resources>

MainActivity.java

package com.example.clixin.justjava;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

    int quantity = 1;

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

    public void submitOrder(View view) {
        CheckBox whippiedCreamCheckBox = (CheckBox) findViewById(R.id.whippied_cream_check_box);
        boolean hasWhippiedCream = whippiedCreamCheckBox.isChecked();
        CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_check_box);
        boolean hasChocolate = chocolateCheckBox.isChecked();
        EditText nameEditText = (EditText) findViewById(R.id.name_edit_text);
        String name = nameEditText.getText().toString();
        int price = calculatePrice(hasWhippiedCream, hasChocolate);
        String priceMassage = getOrderSummary(price, hasWhippiedCream, hasChocolate, name);

        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:"));
        intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.just_order_for) + name);
        intent.putExtra(Intent.EXTRA_TEXT, priceMassage);
        if(intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }

    }

    public void increment(View view) {

        if(quantity == 20) {
            Toast.makeText(this, getString(R.string.not_more_than_20), Toast.LENGTH_SHORT).show();
            return;
        }
        quantity = quantity + 1;
        display(quantity);

    }

    public void decrement(View view) {
        if(quantity == 1) {
            Toast.makeText(this, getString(R.string.not_less_than_1), Toast.LENGTH_SHORT).show();
            return;
        }
        quantity = quantity - 1;
        display(quantity);

    }

    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }

    private int calculatePrice(boolean hasWippiedCream, boolean hasChocolate) {
        int basePrice = 5;

        if (hasWippiedCream) {
            basePrice += 1;
        }

        if (hasChocolate) {
            basePrice += 2;
        }
        return quantity * basePrice;
    }

    private String getOrderSummary(int price, boolean hasWhippiedCream, boolean hasChocolate, String name) {
        String summary = getString(R.string.order_summary_name) + name;
        summary += "\n" + getString(R.string.order_summary_quantity) + quantity;
        summary += "\n" + getString(R.string.order_summary_whippied_cream) + hasWhippiedCream;
        summary += "\n" + getString(R.string.order_summary_chocolate) + hasChocolate;
        summary += "\n" + getString(R.string.order_summary_total) + NumberFormat.getCurrencyInstance().format(price);
        summary += "\n" + getString(R.string.thank_you);
        return summary;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市箭阶,隨后出現(xiàn)的幾起案子虚茶,更是在濱河造成了極大的恐慌戈鲁,老刑警劉巖仇参,帶你破解...
    沈念sama閱讀 223,126評論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異婆殿,居然都是意外死亡诈乒,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評論 3 400
  • 文/潘曉璐 我一進(jìn)店門婆芦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來怕磨,“玉大人,你說我怎么就攤上這事消约〕辏” “怎么了?”我有些...
    開封第一講書人閱讀 169,941評論 0 366
  • 文/不壞的土叔 我叫張陵或粮,是天一觀的道長导饲。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么渣锦? 我笑而不...
    開封第一講書人閱讀 60,294評論 1 300
  • 正文 為了忘掉前任硝岗,我火速辦了婚禮,結(jié)果婚禮上袋毙,老公的妹妹穿的比我還像新娘型檀。我一直安慰自己,他們只是感情好听盖,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,295評論 6 398
  • 文/花漫 我一把揭開白布胀溺。 她就那樣靜靜地躺著,像睡著了一般皆看。 火紅的嫁衣襯著肌膚如雪月幌。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,874評論 1 314
  • 那天悬蔽,我揣著相機(jī)與錄音扯躺,去河邊找鬼。 笑死蝎困,一個(gè)胖子當(dāng)著我的面吹牛录语,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播禾乘,決...
    沈念sama閱讀 41,285評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼澎埠,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了始藕?” 一聲冷哼從身側(cè)響起蒲稳,我...
    開封第一講書人閱讀 40,249評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎伍派,沒想到半個(gè)月后江耀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,760評論 1 321
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡诉植,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,840評論 3 343
  • 正文 我和宋清朗相戀三年祥国,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片晾腔。...
    茶點(diǎn)故事閱讀 40,973評論 1 354
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡舌稀,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出灼擂,到底是詐尸還是另有隱情壁查,我是刑警寧澤,帶...
    沈念sama閱讀 36,631評論 5 351
  • 正文 年R本政府宣布剔应,位于F島的核電站睡腿,受9級特大地震影響康谆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嫉到,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,315評論 3 336
  • 文/蒙蒙 一沃暗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧何恶,春花似錦孽锥、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至疫赎,卻和暖如春盛撑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背捧搞。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評論 1 275
  • 我被黑心中介騙來泰國打工抵卫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人胎撇。 一個(gè)月前我還...
    沈念sama閱讀 49,431評論 3 379
  • 正文 我出身青樓介粘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親晚树。 傳聞我的和親對象是個(gè)殘疾皇子姻采,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,982評論 2 361

推薦閱讀更多精彩內(nèi)容