如何在不使用TextView的情况下使AlertDialog中的链接可单击?

时间:2015-07-13 19:44:44

标签: java android

我在 strings.xml 文件中有这一行:

<string name="backup_help_body">Please watch this video if you did not understand the above steps: www.youtube.com</string>

以及 AlertDialog 中的上述文字:

AlertDialog.Builder backupHelpDialog = new AlertDialog.Builder(this);
                backupHelpDialog.setTitle(getString(R.string.backup_help_title));
                backupHelpDialog.setMessage(R.string.backup_help_body);
                backupHelpDialog.setIcon(R.drawable.my_notes);
                backupHelpDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface backupHelpDialog, int witch) {
                        // Do Not Do Anything.
                    }
                });

                backupHelpDialog.show();
                return true;

如何在不使用 TextView 的情况下在 AlertDialog 中点击链接?!

3 个答案:

答案 0 :(得分:0)

使用HTML格式:

示例:

<string name="my_link"><a href="http://somesite.com/">Click me!</a></string>

供参考:

Android-Is it possible to add a clickable link into a string resource

现在要将链接设为可点击,您需要将LinkMovementMethod添加到相应的Textview。

要获取相关的Textview,您需要使用反射从AlertDialog获取mAlert字段,然后使用findviewbyId(android.R.id.alertTitle)

参考:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.0_r1/android/app/AlertDialog.java#AlertDialog.0mAlert

答案 1 :(得分:0)

我不确定你能不能使用TextView,我会使用自定义AlertDialog,但你可以在代码中创建一个“临时”TextView来执行你需要的东西,这里我留下你有几个例子的url

How can I get clickable hyperlinks in AlertDialog from a string resource?

答案 2 :(得分:0)

我认为最好和最快的方式(我做的)是添加带有textview的自定义布局,然后在该textview上添加onClickListener。

#SuperEasyToDo