Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));//调用邮箱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;
}
}