【发布时间】:2018-06-10 07:29:36
【问题描述】:
我正在制作一个表单应用程序,用于收集信息并通过电子邮件发送。 MainActivity 收集电话号码,然后是警告消息活动,然后是名称活动等。我遇到的问题是将从 EditText 字段收集的数据发送到要作为电子邮件发送的最终协议活动对我不起作用。我到处寻找,但我无法弄清楚如何将用户发送到下一个活动,同时将输入数据发送到最终协议活动,以便可以通过电子邮件发送。
这是我目前所拥有的。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the next button
final Button next1 = (Button) findViewById(R.id.button);
// Find the Edit Text Field to input Phone Number
final EditText phoneField = (EditText) findViewById(R.id.edit_text_phone);
// Set a click listener on the next button
next1.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the next1 Button is clicked on.
@Override
public void onClick(View view) {
// sends data collected from edit text box to be sent to final page so it can be
// sent in an email message.
Intent phoneNumber = new Intent(MainActivity.this, AgreementActivity.class);
phoneNumber.putExtra("phoneMessage", phoneField.getText().toString());
startActivity(phoneNumber);
//starts next activity
Intent nextButton = new Intent(MainActivity.this, Warning.class);
startActivity(nextButton);
}
});
}
}
这是发送电子邮件的最终协议活动。 但是邮件主题行中的最终结果是“您的电话号码为空”
public class AgreementActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.agreement_activity);
// Find the submit button
final Button submit = (Button) findViewById(R.id.button6);
// This is the input from user for phone number field
final Bundle phoneNumber = getIntent().getExtras();
// Set a click listener on that View
submit.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the Submit Button is clicked on.
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.getBundleExtra("phoneMessage");
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Phone Number is " + phoneNumber);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
});
}
}
【问题讨论】:
-
不清楚那里问的是什么。
-
如果你有一个活动而不是三个,这会简单得多。话虽这么说......在
phoneNumber.putExtra("phoneMessage", phoneField.getText().toString())中,您正在将一个名为phoneMessage的额外附加到Intent。该值为String。在intent.getBundleExtra("phoneMessage")中,您试图检索一个名为phoneMessage的Bundle作为额外的。但是,没有名为phoneMessage的Bundle。作为Kehinde points out,你需要使用getStringExtra(),而不是getBundleExtra()。 -
是的,我同意滚动的一项活动会更容易,但由于我希望用户一次将信息输入到表单一个屏幕,因此我进行了多项活动。我确信有更好的方法。不过,我只编码了 1 个月。
标签: java android email android-intent android-activity