SJ64 资源调用,完善JUST JAVA

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));//调用邮箱App

完成Just Java ——源码如下

SJ64 资源调用,完善JUST JAVA_第1张图片
SJ64 资源调用,完善JUST JAVA_第2张图片

activity_main.xml




    

        

        

        

        

        

        

            

Style.xml



    
    
    
    


String.xml


    Just Java
    Type your name here
    Toppings
    Whippied Cream / 1¥
    chocolate / 2¥
    Quantity
    -
    1
    +
    Order Summary
    ¥0
    Order
    Name:
    Has Whippied Cream?
    Has Chocolate?
    total_price:
    Thank you!
    Quantity:
    you can order at least 1 coffee
    you can order at most 20 coffees
    Just order for

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;
    }
}

你可能感兴趣的:(SJ64 资源调用,完善JUST JAVA)