Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
textview Android的多个超链接单击_Android - Fatal编程技术网

textview Android的多个超链接单击

textview Android的多个超链接单击,android,Android,我的数据库中有一个字符串,如下所示 Sring a = "Victory to the <a href='word1'>GOD<\/a>, renowned in <a href='word2'>all three worlds!<\/a>"; text1.setText(Html.fromHtml(a)); Sring a=“胜利归于上帝,享誉三界!”; text1.setText(Html.fromHtml(a)); 现在我需要link的超

我的数据库中有一个字符串,如下所示

Sring a = "Victory to the <a href='word1'>GOD<\/a>, renowned in <a href='word2'>all three worlds!<\/a>";
text1.setText(Html.fromHtml(a));
Sring a=“胜利归于上帝,享誉三界!”;
text1.setText(Html.fromHtml(a));

现在我需要
link
的超链接id(
word1
word2
),这将有助于设置另一个textView属性。谁能告诉我怎么做?或者任何其他方法来实现这一点?

使用子字符串方法来标识ID对href='位置的搜索。创建一个包含子字符串的临时变量:

"word1'>GOD<\/a>, renowned in <a href='word2'>all three worlds!<\/a>"
“word1'>上帝,在三个世界中都享有盛名!”
然后搜索'的位置,从临时变量的第一个字符开始搜索子字符串,直到'的位置包含您的ID

接下来,将此子字符串放入临时变量中:

>GOD<\/a>, renowned in <a href='word2'>all three worlds!<\/a>"
>上帝,在三个世界中都享有盛名!"

现在,您可以重复前面的步骤来接收第二个ID,好的。我现在想到了我的两个解决方案:

1.实现您自己的模式:

您首先需要将所有的
http://
(或
https//
,以及您捕获的URL模式)替换为自定义模式
customSchema://

a.replaceAll("[\\"\']{1}[a-zA-Z]+:\/\/[\\"\']{1}", "customSchema://");
(我不确定我的正则表达式,我只是在这里写)

然后声明您的活动可以在AndroidManifest.xml中处理此类架构:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="customSchema" />
</intent-filter>
如果是您的活动启动了单击,请参阅

2.第二种解决方案在我看来是最简单的:

将字符串拆分为单独的字符串,目的是在不同的文本视图中隔离“word1”、“word2”,如下所示:

<TextView 1 (Victory to the) /><TexTview 2 (word1 GOD) /><TextView 3 (renowned in) /><TextView 4 (word2 all three worlds) />

使用正则表达式可以很容易地做到这一点

您可以将word1和word2设置为标记(
setTag()
)中的TextView 2和TextView 4,以便以后检索


在TextView 2和TextView 4上注册一个onClick事件,并在这些事件中执行您想要的操作(在这里您可以得到您的标记(
getTag()

我使用Webview而不是TextView实现了这一点,并创建了自定义Webview客户端来覆盖url

webview1.setWebViewClient(new myWebViewClient());
webview1.loadData(myHTMLStringWithHyperlinks, "text/html", "UTF-8");

private class myWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            Log.i(TAG, "Id of hyperlink text is: "+url);

            }

            return true;
        }

    }

“word1和word2的id”是什么意思?在HashMap中存储字符串链接并动态构建a如何?请查看编辑的内容。我的目标是,字符串word1在textview中显示为超链接,而用户单击该文本(word1)它将设置为另一个文本视图。您是否控制href字符串?对于每个href,如何使用按钮或单独的文本视图进行控制?是的,您可以看到它。我将使用text1.setText(Html.fromHtml(a))分配它;我想点击textview的每个超链接。是的,我明白了,我的意思是你是否控制了字符串a源并精确地输入了“”,你如何检索这个字符串?你能再检查一遍吗?
webview1.setWebViewClient(new myWebViewClient());
webview1.loadData(myHTMLStringWithHyperlinks, "text/html", "UTF-8");

private class myWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            Log.i(TAG, "Id of hyperlink text is: "+url);

            }

            return true;
        }

    }