2016-12-13 2 views
0

Я хочу отправить электронную почту в мое приложение, я пишу ниже код, и я хочу предоставить тему и адрес электронной почты от EditText и отправить этот текст в почтовый клиент! но при использовании этого и перейдите к почтовому клиенту, не отправляя текст!Как я могу отправить электронную почту в Android

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[]{"[email protected]"}); 
          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

Что именно не работает? –

ответ

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

Эти инструкции выполняются при создании диалога. Пользователь еще ничего не набрал.

Вам необходимо получить введенный текст внутри вашего метода onClick(), поскольку он выполняется, когда пользователь нажимает кнопку «отправить».

+0

как решить мою проблему, мой друг? вы можете мне помочь и отправить код? пожалуйста –

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[]{"[email protected]"}); 
          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); 
          } 
         } 
        } 
       }); 
Смежные вопросы