单击按钮时Android会打开电子邮件应用程序

时间:2014-07-23 21:47:44

标签: android android-intent

我是Android新手。这是应用程序的整个代码,它在点击时不响应按钮。我已经发了邮件的意图。它不会抛出任何异常。如果我只是在文本视图中输出它,它会在单击按钮时显示名称。

package com.rafay.myfirstproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Papi extends Activity implements View.OnClickListener
{
    EditText personsEmail, intro, personsName, stupidThings, hatefulAction,
    outro;
    Intent emailIntent;
TextView check;
String emailAdd, beginning, name, stupidAction, hatefulAct, out;
Button sendEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.email);
        //EditText personsEmail, intro, personsName, stupidThings, hatefulAction,
        //outro,check;
        //Button sendEmail;
         personsEmail = (EditText) findViewById(R.id.etEmails);
         intro = (EditText) findViewById(R.id.etIntro);
         personsName = (EditText) findViewById(R.id.etName);
         stupidThings = (EditText) findViewById(R.id.etThings);
         hatefulAction = (EditText) findViewById(R.id.etAction);
        outro = (EditText) findViewById(R.id.etOutro);
        sendEmail = (Button) findViewById(R.id.bSentEmail);
         check = (TextView) findViewById(R.id.tvCheck);
         sendEmail.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated();
        String emailaddress[] = { emailAdd };
        String message = "Well hello " + name;
        try{
        emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_EMAIL  , emailaddress);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        emailIntent.putExtra(Intent.EXTRA_TEXT   , message);
        v.getContext().startActivity(emailIntent);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    private void convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated() {
        // TODO Auto-generated method stub
            name = personsName.getText().toString();
        }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}

1 个答案:

答案 0 :(得分:0)

设置intent type并在方法结束时调用startActivity()

public void onClick(View v) {

    ...
    try{

        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_EMAIL  , emailaddress);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        emailIntent.putExtra(Intent.EXTRA_TEXT   , message);

        emailIntent.setType("text/plain"); // <-- HERE
        startActivity(emailIntent); // <-- AND HERE

    }

    ... 
}
相关问题