0

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

Sring a = "Victory to the <a href='word1'>GOD<\/a>, renowned in <a href='word2'>all three worlds!<\/a>";
text1.setText(Html.fromHtml(a));

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

4

3 回答 3

1

我已经使用 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;
        }

    }
于 2013-02-18T09:48:30.153 回答
0

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

"word1'>GOD<\/a>, renowned in <a href='word2'>all three worlds!<\/a>"

然后从 temp-vars 第一个字符中搜索' SubString 的位置,直到'的位置包含您的 ID

接下来你把这个 SubString 放到 temp-var 中:

>GOD<\/a>, renowned in <a href='word2'>all three worlds!<\/a>"

现在您可以重复前面的步骤来接收第二个 ID

于 2013-02-15T11:19:46.480 回答
0

Oki 这里是我目前想到的 2 个解决方案:

1.实现自己的架构:

您首先需要用您的自定义架构替换所有http://(或者https//,您捕获的 URL 架构)customSchema://

a.replaceAll("[\\"\']{1}[a-zA-Z]+:\/\/[\\"\']{1}", "customSchema://");

(我不确定我的正则表达式,我只是写在这里)

然后你声明你的 Activity 可以在你的 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>

此时,将为每个单击的 customSchema://... URL 启动您的活动(即使在其他应用程序中)

您只需检索 Activity 上的 URL 并使用它做您想做的事情:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  // Get the intent that started this activity
  Intent intent = getIntent();
  Uri data = intent.getData();
}

如果是您的 Activity 启动了点击,请参阅onNewIntent

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

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

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

您可以使用正则表达式轻松做到这一点

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

在您的 TextView 2 和 TextView 4 上注册一个 onClick 事件并在其中执行您想要的操作(在这里您可以获取您的标签 ( getTag())

于 2013-02-15T14:30:35.893 回答