将textview的内容传递给邮件正文

时间:2018-01-06 11:53:01

标签: android email android-intent textview

我想将textview的内容传递给邮件正文  该按钮适用于打开和发送邮件, 但是如何在邮件正文中添加textview?

enter code  public class Buy1Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    ImageUpload imgUploadObj = getIntent().getExtras().getParcelable("selected_image_upload");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_buy1);
    TextView txa = (TextView) findViewById(tx);




    txa.setText( " Name : " +
            imgUploadObj.getName() + "\n" + " Brand : " + imgUploadObj.getBrand() +
            "\n" + " Model : " + imgUploadObj.getModel() + "\n" + " Year : " +
            imgUploadObj.getYear() + "\n" + " price : " + imgUploadObj.getPrice() +
            "\n" + " desc + : " + imgUploadObj.getDesc());


    Button startBtn = (Button) findViewById(R.id.sendEmail1);

    startBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            sendEmail();
        }
    });

}




protected void sendEmail() {
    Log.i("Send email", "");
    String[] TO = {"201416134@omancollege.edu.om"};
    String[] CC = {""};


    Intent emailIntent = new Intent(Intent.ACTION_SEND);

    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
    emailIntent.putExtra(Intent.EXTRA_CC, CC);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "whant to buy item");





    try {
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        finish();
        Log.i(" Mail sent...", "");
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(Buy1Activity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
    }
}

}这里

我的xml

enter code <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<TextView
    android:id="@+id/tx"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:textStyle="bold"
    android:text="TextView" />



<Button
    android:id="@+id/sendEmail1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/compose_email"/>

这里   这是我的完整的java和xml代码,没有编辑任何东西也许我会导致任何错误,因为我是一个非常初学者

2 个答案:

答案 0 :(得分:1)

如果您想获取TextView的内容并作为邮件正文发送,那么您确实喜欢这样: -

首先获取textview的内容并存储在String

TextView txa = (TextView) findViewById(R.id.tx);

String textViewContent = txa.getText().toString();

然后您可以将其作为邮件正文发送:

emailIntent.putExtra(Intent.EXTRA_TEXT,textViewContent);

我已编辑了您的代码,请使用此代码并再次运行您的应用

 public class Buy1Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_buy1);
    TextView txa = (TextView) findViewById(tx);


 ImageUpload imgUploadObj = getIntent().getExtras().getParcelable("selected_image_upload");


    txa.setText( " Name : " +
            imgUploadObj.getName() + "\n" + " Brand : " + imgUploadObj.getBrand() +
            "\n" + " Model : " + imgUploadObj.getModel() + "\n" + " Year : " +
            imgUploadObj.getYear() + "\n" + " price : " + imgUploadObj.getPrice() +
            "\n" + " desc + : " + imgUploadObj.getDesc());


    Button startBtn = (Button) findViewById(R.id.sendEmail1);

    startBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            sendEmail(txa.getText().toString());
        }
    });

}




protected void sendEmail(String yourContent) {
    Log.i("Send email", "");
    String[] TO = {"201416134@omancollege.edu.om"};
    String[] CC = {""};


    Intent emailIntent = new Intent(Intent.ACTION_SEND);

    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
    emailIntent.putExtra(Intent.EXTRA_CC, CC);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "whant to buy item");
    emailIntent.putExtra(Intent.EXTRA_TEXT,yourContent);





    try {
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        finish();
        Log.i(" Mail sent...", "");
    } catch (Exception ex) {
        Toast.makeText(Buy1Activity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
    }
}
}

答案 1 :(得分:0)

试试这个

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,           “电子邮件正文”);