gpt4 book ai didi

android - 在 AlertDialog 中打开电子邮件客户端

转载 作者:行者123 更新时间:2023-11-29 19:29:35 25 4
gpt4 key购买 nike

我想在 AlertDialog 窗口中单击文本时打开电子邮件客户端。我使用了 Setspan 方法和 Linkify 但没有结果。这是我的代码:

SpannableString s = new SpannableString("contact");
s.setSpan(new URLSpan("http://www.google.com"), 0, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
final TextView tx1 = new TextView(this);
tx1.setText(getString(R.string.locerstatus) +s);
tx1.setAutoLinkMask(RESULT_OK);
tx1.setMovementMethod(LinkMovementMethod.getInstance());

Linkify.addLinks(s, Linkify.EMAIL_ADDRESSES);
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) { }
})
.setView(tx1).show();

最佳答案

尝试使用ClickableSpan像这样:

final TextView tx1 = new TextView(this);

tx1.setAutoLinkMask(RESULT_OK);
tx1.setMovementMethod(LinkMovementMethod.getInstance());

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
})
.setView(tx1);

final AlertDialog dialog = builder.create();

String sourceString = "This is test string with contact word";
String keyWord = "contact";
SpannableString spannableString = new SpannableString(sourceString);

ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
dialog.dismiss();
}
};
spannableString.setSpan(clickableSpan, sourceString.indexOf(keyWord), sourceString.indexOf(keyWord) + keyWord.length(), 0);

tx1.setText(spannableString);

dialog.show();

更新

如果您想更改可点击文本的颜色,您还应该重写 ClickableSpan()updateDrawState() 方法,如下所示:

ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
dialog.dismiss();
}

@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.BLUE);
ds.setUnderlineText(true);
}
};

关于android - 在 AlertDialog 中打开电子邮件客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40386812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com