将 textview 的内容传递给邮件正文
Posted
技术标签:
【中文标题】将 textview 的内容传递给邮件正文【英文标题】:Pass content of textview to mail message body 【发布时间】:2018-06-16 01:28:41 【问题描述】:我想将 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_
android:layout_
android:orientation="vertical" >
<TextView
android:id="@+id/tx"
android:layout_
android:layout_
android:textStyle="bold"
android:text="TextView" />
<Button
android:id="@+id/sendEmail1"
android:layout_
android:layout_
android:text="@string/compose_email"/>
这里 这是我完整的 java 和 xml 代码,没有编辑任何东西,也许我这样做会导致任何错误,因为我是初学者
【问题讨论】:
你要传递这个文本-TextView txa。进入您的邮件正文 是的,此文本的内容已经从 listview 传递,然后我希望 textview 的内容在我的消息正文中 mohamed 检查我的以下答案, 并且您错误地使用了 textview 参考,请使用下面的代码回答 TextView txa = (TextView) findViewById(R.id.tx);代替您的旧代码 TextView txa = (TextView) findViewById(tx); 嗨 Mohanmed,请分享您的错误和代码 【参考方案1】:如果您想获取 TextView 的内容并作为邮件正文发送,那么您可以这样做:-
首先获取文本视图的内容并存储在字符串中
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();
【讨论】:
对不起,不,它不工作,它没有显示错误,但是当我打开这个活动时应用程序关闭 向我展示您的完整 java 和 xml 代码,请显示您遇到的错误,以便我可以在这里解决您的问题【参考方案2】:试试这个
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, “电子邮件正文”);
【讨论】:
以上是关于将 textview 的内容传递给邮件正文的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Spring Integration Mail 从“text/html; charset=us-ascii”检索电子邮件内容正文