从活动之间传递字符串和字符串构建器

时间:2013-07-20 09:39:47

标签: android string email android-intent

我有两个活动,我需要将一些字符串和一个字符串构建器从TailorThreeActivity.java传递给EnquireActivity.java。

在EnquireActivity屏幕上显示字符串和字符串构建器数据后,我通过单击“发送邮件”按钮调用Intent来发送邮件。 “发送邮件”按钮将打开邮件客户端,并将所有字符串和字符串构建器数据设置为电子邮件正文。

我能够在EnquireActivity.java中显示stringbuilder的值,但来自TailorThreeActivity.java的其他字符串不会显示在活动中。尽管在EnquireActivity中显示了字符串构建器,但字符串和字符串构建器都不会显示在电子邮件消息体中。

这是TailorThreeActivity.java

package com.example.travelplanner;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TailoredthreeActivity extends Activity implements OnClickListener{

    TextView txtday1, txtmon1, txtday2, txtmon2;
    EditText et_name,et_email,et_adult,et_child,et_phone;
    Button btn;
    private int year,year1;
    private int month,month1;
    private int day,day1;
    String date1, date2;
    ArrayList<String> getChecked;
    static final int DATE_DIALOG_ID = 0;
    static final int DATE2_DIALOG_ID = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_tailoredthree);
        et_name = (EditText) findViewById(R.id.editname);
        et_adult = (EditText) findViewById(R.id.editno_adult);
        et_child = (EditText) findViewById(R.id.editno_child);
        et_email = (EditText) findViewById(R.id.editmail);
        et_phone = (EditText) findViewById(R.id.editphone);
        txtday1 = (TextView) findViewById(R.id.txtdate_day);
        txtday2 = (TextView) findViewById(R.id.txtdate_day1);
        txtmon1 = (TextView) findViewById(R.id.txtdate_mon_year);
        txtmon2 = (TextView) findViewById(R.id.txtdate_mon_year1);
        btn = (Button)findViewById(R.id.btn_tailorthree_verify);
        btn.setOnClickListener(this);

        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        year1 = c.get(Calendar.YEAR);
        month1 = c.get(Calendar.MONTH);
        day1 = c.get(Calendar.DAY_OF_MONTH);

        //get Intents

         Bundle extras = getIntent().getExtras();
            if(extras!=null)
            {
              getChecked = extras.getStringArrayList("list");
           }

    }

    @SuppressWarnings("deprecation")
    public void showdate(View v) {
        showDialog(DATE_DIALOG_ID);

    }

    @SuppressWarnings("deprecation")
    public void showdate1(View v) {
        showDialog(DATE2_DIALOG_ID);
    }


    public void updateDate() {
        final String[] MONTHS = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        String mon = MONTHS[month]; 
        txtday1.setText(""+day);
        txtmon1.setText(mon+"'"+year);
        }

    public void updateDate1() {
        final String[] MONTHS = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        String mon = MONTHS[month]; 
        txtday2.setText(""+day);
        txtmon2.setText(mon+"'"+year);
        }

    private DatePickerDialog.OnDateSetListener datedialog = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int yy, int mm,
                int dd) {
            // TODO Auto-generated method stub
            month = mm;
            day = dd;
            year = yy;
            date1 = day+"/"+month+"/"+year;
            updateDate();
        }
    };

private DatePickerDialog.OnDateSetListener datedialog2 = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int yy, int mm,
                int dd) {
            // TODO Auto-generated method stub
            month1 = mm;
            day1 = dd;
            year1 = yy;
            date2 = day1+"/"+month1+"/"+year1;
            updateDate1();
        }
    };

    protected Dialog onCreateDialog(int id) {
        //new DatePick
         switch (id) {  
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                        datedialog, year, month,day);

            case DATE2_DIALOG_ID:
                return new DatePickerDialog(this,
                        datedialog2, year1, month1,day1);
         }
         return null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tailoredthree, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i1 = new Intent(this, EnquireActivity.class);
        i1.putExtra(et_name.getText().toString(), "name");
        i1.putExtra(et_adult.getText().toString(), "adults");
        i1.putExtra(et_child.getText().toString(), "child");
        i1.putExtra(et_email.getText().toString(), "email");
        i1.putExtra(et_phone.getText().toString(), "phone");
        i1.putExtra(date1, "datedept");
        i1.putExtra(date2, "datearr");
        i1.putStringArrayListExtra("list1", getChecked);
        startActivity(i1);
    }

}

这是EnquireActivity.java

package com.example.travelplanner;

import java.util.ArrayList;
import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class EnquireActivity extends Activity implements OnClickListener, OnCheckedChangeListener {

    //setting var for datepicker
    private int year,year1;
    private int month,month1;
    private int day,day1;
    String date1, date2;
    static final int DATE_DIALOG_ID = 0;
    static final int DATE2_DIALOG_ID = 1;

    Button btn;
    StringBuilder stringbuilder, mailbuilder;
    String to, subject,msg;
    String getname,getadult, getchild, getmail, getphone, getdatedept, getdatearr;
    ArrayList<String> getChecked;
    CheckBox chk;
    EditText et_citylist, et_name, et_adult, et_child, et_mail, et_phone, et_datedepart, et_datearrive;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enquire);
        //registering edittexts
        et_citylist = (EditText)findViewById(R.id.enquire_edit_city);
        et_name = (EditText)findViewById(R.id.enquire_edit_name);
        et_adult = (EditText)findViewById(R.id.enquire_edit_adult);
        et_child = (EditText)findViewById(R.id.enquire_edit_child);
        et_mail = (EditText)findViewById(R.id.enquire_edit_email);
        et_phone = (EditText)findViewById(R.id.enquire_edit_phone);
        et_datearrive = (EditText)findViewById(R.id.enquire_edit_datearrival);
        et_datedepart = (EditText)findViewById(R.id.enquire_edit_datedeparture);

        //setting up email button
        btn = (Button)findViewById(R.id.btnemail);
        btn.setVisibility(View.GONE);
        btn.setOnClickListener(this);

        //setting up mail content
        to = "divyangbhambhani@gmail.com";
        subject = "Tour Package Details Summary";
        msg = "Tour Enquiry Details:\nName: "+et_name.getText().toString()+"\nDestinations: "+et_citylist.getText().toString()+"\nNo. of Adult Members: "+et_adult.getText().toString()+"\nNo. of Child Members: "+et_child.getText().toString()+"\nDeparture: "+et_datedepart.getText().toString()+"\nArrival: "+et_datearrive.getText().toString()+"\nEmail: "+et_mail.getText().toString()+"\nContact No.: "+et_phone.getText().toString();

        //setting checkbox
        chk = (CheckBox)findViewById(R.id.chkenquire);
        chk.setOnCheckedChangeListener(this);

        //setting calender components for datepicker
        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        year1 = c.get(Calendar.YEAR);
        month1 = c.get(Calendar.MONTH);
        day1 = c.get(Calendar.DAY_OF_MONTH);

        //getting Intents from previous activities

        Bundle extras = getIntent().getExtras();
            if(extras!=null)
            {
              getChecked = extras.getStringArrayList("list1");
              getname = extras.getString("name");
              getadult = extras.getString("adults");
              getchild = extras.getString("child");
              getmail = extras.getString("email");
              getphone = extras.getString("phone");
              getdatedept = extras.getString("datedept");
              getdatearr = extras.getString("datearr");
              stringbuilder = new StringBuilder();
              for(String value:getChecked){
                  stringbuilder.append(value).append(" ");
              }
           }
        //adding citylist to edittext   
            et_citylist.setText(stringbuilder);
            et_name.setText(getname);
            et_adult.setText(getadult);
            et_child.setText(getchild);
            et_mail.setText(getmail);
            et_phone.setText(getmail);
            et_datearrive.setText(getdatearr);
            et_datedepart.setText(getdatedept);

    }

    @SuppressWarnings("deprecation")
    public void showdate(View v) {
        showDialog(DATE_DIALOG_ID);

    }

    @SuppressWarnings("deprecation")
    public void showdate1(View v) {
        showDialog(DATE2_DIALOG_ID);
    }

    public void updateDate() {
        final String[] MONTHS = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        String mon = MONTHS[month]; 
        et_datedepart.setText(date1);
        }

    public void updateDate1() {
        final String[] MONTHS = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        String mon = MONTHS[month]; 
        et_datearrive.setText(date2);
        }

    private DatePickerDialog.OnDateSetListener datedialog = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int yy, int mm,
                int dd) {
            // TODO Auto-generated method stub
            month = mm;
            day = dd;
            year = yy;
            date1 = day+"/"+month+"/"+year;
            updateDate();
        }
    };

private DatePickerDialog.OnDateSetListener datedialog2 = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int yy, int mm,
                int dd) {
            // TODO Auto-generated method stub
            month1 = mm;
            day1 = dd;
            year1 = yy;
            date2 = day1+"/"+month1+"/"+year1;
            updateDate1();
        }
    };

    protected Dialog onCreateDialog(int id) {
        //new DatePick
         switch (id) {  
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                        datedialog, year, month,day);

            case DATE2_DIALOG_ID:
                return new DatePickerDialog(this,
                        datedialog2, year1, month1,day1);
         }
         return null;
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.enquire, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //Send Mail
        Intent email = new Intent(Intent.ACTION_SEND);
          email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
          //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
          //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
          email.putExtra(Intent.EXTRA_SUBJECT, subject);
          email.putExtra(Intent.EXTRA_TEXT, msg);

          //need this to prompts email client only
          email.setType("message/rfc822");

          startActivity(Intent.createChooser(email, "Choose an Email client :"));
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if(chk.isChecked())
            btn.setVisibility(View.VISIBLE);
        else
            btn.setVisibility(View.GONE);

    }

}

2 个答案:

答案 0 :(得分:1)

我看到的主要问题之一是代码的这一部分

i1.putExtra(et_name.getText().toString(), "name");
i1.putExtra(et_adult.getText().toString(), "adults");
i1.putExtra(et_child.getText().toString(), "child");
i1.putExtra(et_email.getText().toString(), "email");
i1.putExtra(et_phone.getText().toString(), "phone");

getname = extras.getString("name");
getadult = extras.getString("adults");
getchild = extras.getString("child");
getmail = extras.getString("email");
getphone = extras.getString("phone");

您没有发送这些值的相关性,因为当您使用putExtra时,第一个参数是键,第二个参数是值。这就是为什么你无法在第二次活动中检索它们。

答案 1 :(得分:0)

Intent在第一个活动中包含名称 - 值对正确

 Intent i = new Intent();

 StringBuffer sb = new StringBuffer("Hello");

 i.putExtra("email", "jignesh.patel@y7mail.com"); 

 i.putExtra("sub", sb.toString());

像这样发送..

发送邮件设置这些参数..

 String message = "Your_message"; String mail_id = "Email_id"; String
 sub = "Mail Subject";

 Intent mail = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" +
 mail_id));

 mail.putExtra(Intent.EXTRA_SUBJECT,sub);
 mail.putExtra(Intent.EXTRA_TEXT, message);

 startActivity(Intent.createChooser(mail,"Mail Using"));

..它会对你有用..