【问题标题】:how to detect website link in Textview and make it clickable [duplicate]如何在Textview中检测网站链接并使其可点击[重复]
【发布时间】:2017-09-10 00:27:15
【问题描述】:

伙计们,我正在尝试在我的textview 中添加带有网站link 之类的大文本@(https://stackoverflow.com/questions/ask),位于数据库中 我尝试像这张图片一样直接添加它:

我在数据库中的价值图片

但它以Textview 的形式出现在应用程序中,不能像这张图片一样点击:

输出应用

protected void onPostExecute(String result) {

        // Parse les données JSON
        String id;
        String solution;
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                id= json_data.getString("alltext");
                solution = json_data.getString("title");
                prob.setText(solution);
                sol.setText(id);
                // Résultats de la requête
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

    }

【问题讨论】:

    标签: android database hyperlink textview


    【解决方案1】:

    TextView中的AutoLink设置为web会自动检测链接

    <TextView
        android:autoLink="web"
        android:text="This is Google http://www.google.com"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

    【讨论】:

    • 非常感谢,请问我还有一个问题,我想制作超链接,例如:(youtube.com) 将其替换为 Youtube
    • 将此代码用于超链接文本TextView textView =(TextView)findViewById(R.id.textView); textView.setClickable(true); textView.setMovementMethod(LinkMovementMethod.getInstance())‌​; String text = "&lt;a href='http://www.google.com'&gt; Google &lt;/a&gt;"; textView.setText(Html.fromHtml(text));
    • 工作。谢谢。
    【解决方案2】:

    首先将Text设置为TextView

    lbl2.setText("hi http://google.com");
    Linkify.addLinks(lbl2,Linkify.ALL);
    

    自动链接可点击的链接和其他文本保持不变。

    【讨论】:

    • 非常感谢,帮帮我:)
    • 我的荣幸 :) 如果它真的对你有帮助,请标记。
    • 谢谢兄弟!!!@ArslanMaqbool
    • 这是我的荣幸@Divyaraj
    猜你喜欢
    • 2011-02-13
    • 2022-12-10
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 2013-04-12
    • 2013-08-15
    相关资源
    最近更新 更多