简体   繁体   中英

How to send date/time picker as an e-mail intent

I'm trying to build an app to make my whife's work easier. The app should work like this: Collect all the information from a customer and send it to my wife e-mail address. Most of it i succeeded but i'stuck at the date and time picker and it seems that the date and the hour is not displayed in the e-mail body. I can't figure out what i'm doing wrong. Go easy on me because i'm a beginner.
Here you have the code for my app:

    package com.example.android.manichiura;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.CheckBox;
import android.widget.TimePicker;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import static java.lang.String.valueOf;

/**
 * This app displays an order form to order services from a beauty parlour.
 */
public class MainActivity extends AppCompatActivity implements
        View.OnClickListener {

    int quantity = 0;
    Button btnDatePicker, btnTimePicker;
    EditText txtDate, txtTime;
    private int mYear, mMonth, mDay, mHour, mMinute;

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

        btnDatePicker = (Button) findViewById(R.id.btn_date);
        btnTimePicker = (Button) findViewById(R.id.btn_time);
        txtDate = (EditText) findViewById(R.id.in_date);
        txtTime = (EditText) findViewById(R.id.in_time);


        btnDatePicker.setOnClickListener(this);
        btnTimePicker.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        if (v == btnDatePicker) {

            // Get Current Date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);


            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {

                            txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
        }
        if (v == btnTimePicker) {

            // Get Current Time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            // Launch Time Picker Dialog
            TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {

                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {

                            txtTime.setText(hourOfDay + ":" + minute);
                        }
                    }, mHour, mMinute, true);
            timePickerDialog.show();
        }
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        String inputDate = txtDate.getText().toString();
        String inputTime = txtTime.getText().toString();

        //Gets the name input from the user
        EditText inputName = (EditText) findViewById(R.id.name_input);
        String fillForm = inputName.getText().toString();
            //Gets phonenumber from the user
        EditText inputPhone = (EditText) findViewById(R.id.phone_input);
        String phoneForm = inputPhone.getText().toString();
        //figures out if the user wants manichiura
        CheckBox manichiura = (CheckBox) findViewById(R.id.manichiura);
        boolean hasManichiura = manichiura.isChecked();
        //figures out if the user wants pedichiura
        CheckBox pedichiura = (CheckBox) findViewById(R.id.pedichiura);
        boolean hasPedichiura = pedichiura.isChecked();
        //Figure out if the user wants constructie
        CheckBox constructie = (CheckBox) findViewById(R.id.constructie);
        boolean hasConstructie = constructie.isChecked();
        //figures out if the user wants intretinere
        CheckBox intretinere = (CheckBox) findViewById(R.id.intretinere);
        boolean hasIntretinere = intretinere.isChecked();
        int price = calculatePrice(hasConstructie, hasIntretinere, hasManichiura, hasPedichiura);
        String priceMessage = createOrderSummary(price,  hasConstructie, hasIntretinere,
                hasManichiura, hasPedichiura, fillForm, phoneForm, inputDate, inputTime);
        if ((fillForm.length() > 0) && (phoneForm.length() > 0)){
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("mailto:")); // only email apps should handle this
            intent.putExtra(Intent.EXTRA_EMAIL, "example@gmail.com");
            intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.order_summary_email_subject));
            intent.putExtra(Intent.EXTRA_TEXT, priceMessage);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }
        } else {
            Toast.makeText(this, getString(R.string.detailes_field_blank),
                    Toast.LENGTH_LONG).show();
        }


    }

    /**
     * Calculates the price of the order based on the current quantity.
     *
     * @return the price
     */
    private int calculatePrice(boolean addConstructie, boolean addIntretinere,
                               boolean addManichiura, boolean addPedichiura){
        int basePrice = 0;
        int model = quantity * 5;

        if(addManichiura) {
            basePrice = basePrice + 15;
        }

        if(addPedichiura){
            basePrice = basePrice + 20;
        }

        if(addConstructie) {
            basePrice = basePrice + 70;
        }

        if(addIntretinere){
            basePrice = basePrice + 50;
        }

        int price = model + basePrice;
        return price;
    }



    /**
     * create order summary method
     * @param price
     * @param fillForm gets useer input name
     * @param addConstructie is whether or not the user wants whipped cream
     * @param  addIntretinere is whether or not the user wants chocolate
     * @return order summary
     */

    private String createOrderSummary(int price, boolean addConstructie,
                                      boolean addIntretinere, boolean addManichiura,
                                      boolean addPedichiura, String fillForm, String phoneForm,
                                      String inputDate, String inputTime) {
        String priceMessage = getString(R.string.order_sumary_name, fillForm);
        priceMessage += "\n" + getString(R.string.phone_form, phoneForm);
        priceMessage += "\n" + getString(R.string.date_input, inputDate);
        priceMessage += "\n" + getString(R.string.time_input, inputTime);
        priceMessage += "\n";
        priceMessage += "\n" + getString(R.string.order_summary);

            if (addManichiura) {

                priceMessage += "\n" + getString(R.string.manicure);
            }
            else {
            priceMessage += "";
            }

            if (addPedichiura) {
                priceMessage += "\n" + getString(R.string.pedicure);
            }
            else {
                priceMessage += "";
            }

            if (addConstructie) {
                priceMessage += "\n" + getString(R.string.construction);
            }
            else {
                priceMessage += "";
            }

            if (addIntretinere) {
                priceMessage += "\n" + getString(R.string.care);
            }
            else {
                priceMessage += "";
            }
        priceMessage += "\n" + getString(R.string.order_summary_quantity,quantity);
        priceMessage += "\n";
        priceMessage += "\n" + getString(R.string.order_summary_price, price);
        priceMessage += "\n" + getString(R.string.thank_you);
        return priceMessage;
    }

    /**
     * This method is called when the plus button is clicked.
     */
    public void increment(View view) {
        if (quantity == 10) {
            Toast.makeText(this, getString(R.string.minimum_model),
                    Toast.LENGTH_SHORT).show();
            return;
        }
        quantity = quantity + 1;
        displayQuantity(quantity);
    }

    /**
     * This method is called when the minus button is clicked.
     */
    public void decrement(View view) {
        if (quantity == 0) {
            Toast.makeText(this, getString(R.string.maximum_model),
                    Toast.LENGTH_SHORT).show();
            return;
        }
        quantity = quantity - 1;
        displayQuantity(quantity);
    }

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


}

Please check these two lines.

 int price = calculatePrice(hasConstructie, hasIntretinere, hasManichiura, hasPedichiura);
 String priceMessage = createOrderSummary(price,  hasConstructie, hasIntretinere,
                hasManichiura, hasPedichiura, fillForm, phoneForm);

i think there may be some problems when you are passing argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM