当我将它输出到电子邮件时,我试图更改文本(字符串)的颜色。我的代码是:
String appdata = "%" + txtFromSpinner + location.getText() + "%" + date.getText()+ "%" + start.getText() + "%" + finish.getText() + "%" + lunch.getText() + "%" + details.getText();
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"payrolldirectgib@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Timesheet/Parte de horas");
emailIntent.putExtra(Intent.EXTRA_TEXT, appdata +sep+ "Please send this email."+sep+ "Your timesheet details are included in it."+sep+ "Thank you."+sep+ "Regards,"+sep+ "Admin Department."+sep+ "Payroll Direct.");
emailIntent.setType("message/rfc822");
startActivity(emailIntent);
我希望字符串“appdata”在电子邮件消息框中显示为红色。
这可以做到吗?如何做到?
提前谢谢你。
最佳答案
有两种方法
方法一
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString redSpannable= new SpannableString(appdata);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, appdata.length(), 0);
builder.append(redSpannable);
方法二
appdata_in_red = Html.fromHtml("<font color=#ff0000>" + appdate + "</font>");
我采用了最简单的方法并将其集成到您的代码中,如下所示:
String appdata = "%" + txtFromSpinner + location.getText() + "%" + date.getText()+ "%" + start.getText() + "%" + finish.getText() + "%" + lunch.getText() + "%" + details.getText();
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"payrolldirectgib@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Timesheet/Parte de horas");
//this line below
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<font color=#ff0000>" + appdata + "</font>") +sep+ "Please send this email."+sep+ "Your timesheet details are included in it."+sep+ "Thank you."+sep+ "Regards,"+sep+ "Admin Department."+sep+ "Payroll Direct.");
emailIntent.setType("message/rfc822");
startActivity(emailIntent);
希望我的回答对您有所帮助。
关于java - 发送电子邮件时更改文本(字符串)的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30456896/