如何在Android

时间:2016-12-13 17:56:25

标签: android email

我希望在我的应用程序中发送电子邮件,我在下面写代码,我想从EditText提供主题和电子邮件内容,并将此文本发送到电子邮件客户端!但是当使用它并转到电子邮件客户端而不发送文本时!

Dialog dialog = new Dialog(context);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.dialog_send_idea);
                    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

                    EditText dialogTitle = (EditText) dialog.findViewById(R.id.ideaDialog_title);
                    EditText dialogDescription = (EditText) dialog.findViewById(R.id.ideaDialog_description);
                    Button dialogSend = (Button) dialog.findViewById(R.id.ideaDialog_sendButton);
                    final String emailTitle = dialogTitle.getText().toString();
                    final String emailDescription = dialogDescription.getText().toString();

                    dialogSend.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent sendEmailItent = new Intent(Intent.ACTION_SEND);
                            sendEmailItent.setType("message/rfc822");
                            sendEmailItent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"tellfa.group@gmail.com"});
                            sendEmailItent.putExtra(Intent.EXTRA_SUBJECT, emailTitle);
                            sendEmailItent.putExtra(Intent.EXTRA_TEXT   , emailDescription);
                            try {
                                startActivity(Intent.createChooser(sendEmailItent, "ارسال با : "));
                            } catch (android.content.ActivityNotFoundException ex) {
                                TastyToast.makeText(context, "برنامه مورد نظر یافت نشد", TastyToast.LENGTH_LONG, TastyToast.WARNING);
                            }
                        }
                    });

                    dialog.show();

如何在EditText中向用户提供文本并将其传递到电子邮件客户端?谢谢大家

2 个答案:

答案 0 :(得分:4)

final String emailTitle = dialogTitle.getText().toString();
final String emailDescription = dialogDescription.getText().toString();

创建对话框时正在执行这些语句。用户尚未输入任何内容。

您需要检索onClick()方法中的输入内容,因为当用户点击“发送”按钮时会执行此操作。

答案 1 :(得分:0)

这是@Commonsware的答案看起来像实现的。加上一些错误检查。

     dialogSend.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(dialogTitle.getText().toString().isEmpty(){
                            dialogTitle.setError("Subject must not be empty");
                        }else if(emailDescription.getText().toString().isEmpty()){
                            emailDescription.setError("Content must not be empty");
                        }else{

                            Intent sendEmailIntent = new Intent(Intent.ACTION_SEND);
                            sendEmailIntent.setType("message/rfc822");
                            sendEmailIntent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"tellfa.group@gmail.com"});
                            sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, emailTitle);
                            sendEmailIntent.putExtra(Intent.EXTRA_TEXT   , emailDescription);
                            try {
                                startActivity(Intent.createChooser(sendEmailIntent, "ارسال با : "));
                            } catch (android.content.ActivityNotFoundException ex) {
                                TastyToast.makeText(context, "برنامه مورد نظر یافت نشد", TastyToast.LENGTH_LONG, TastyToast.WARNING);
                            }
                        }
                    }
                });