Android:如何在ListView中添加HTML链接?

时间:2022-06-01 21:11:29

How would I go about adding clickable links inside a ListView?

我如何在ListView中添加可点击链接?

5 个解决方案

#1


22  

This is done using the autoLink attribute of a TextView. Took me some time to dig through the documentation so putting it here with an example in case someone else is looking for it:

这是使用TextView的autoLink属性完成的。花了我一些时间来挖掘文档,所以把它放在这里,以防万一其他人正在寻找它:

Let us assume that you are binding your listview to a custom adapter. In that case, the following piece of code goes into your getView call:

我们假设您将listview绑定到自定义适配器。在这种情况下,以下代码段将进入getView调用:

Code:

textcontent.setText(Html.fromHtml(item.get_text()));
textcontent.setAutoLinkMask(Linkify.WEB_URLS);

Just put the link inside the text being passed to the setText call and you're done.

只需将链接放在要传递给setText调用的文本中,就可以了。

XML:

<TextView
                android:id="@+id/txtview"
                android:autoLink="web"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="put your link here"/>

Hope that helps...

希望有帮助......

#2


17  

If you have text that is already in HTML format, the best thing to do is the following:

如果您的文本已经是HTML格式,那么最好的办法是:

TextView textcontent = (TextView) findViewById(...);
textcontent.setMovementMethod(LinkMovementMethod.getInstance());

String text = "<a href="http://www.*.com">*.com</a>";
textcontent.setText(Html.fromHtml(text));

This will cause any link tags to be clickable in your text view. Alternately, you could use android:autoLink="web" as suggested by Legend, but this has the side-effect of a) linkifying urls that are not wrapped in anchor tags, and b) potentially missing urls or linkifying things that aren't urls. If you want the smarts of autoLink then you should use it, but if all you want is to linkify the tags that are already there, you're better off using setMovementMethod().

这将导致在文本视图中可以单击任何链接标记。或者,您可以按照Legend的建议使用android:autoLink =“web”,但这会产生以下副作用:a)链接未包含在锚标记中的网址,以及b)可能缺少网址或链接不是网址。如果你想要autoLink的智能,那么你应该使用它,但如果你想要的只是链接已经存在的标签,你最好使用setMovementMethod()。

See this bug report for more details: http://code.google.com/p/android/issues/detail?id=2219

有关详细信息,请参阅此错误报告:http://code.google.com/p/android/issues/detail?id = 2219

#3


10  

Hmm, it seems that adding textcontent.setMovementMethod(LinkMovementMethod.getInstance()); makes it so that the clicks on the textview's text parts are no longer passed through to the listview below.

嗯,似乎添加了textcontent.setMovementMethod(LinkMovementMethod.getInstance());这使得textview文本部分的点击不再传递到下面的列表视图。

I found a simple workaround under Issue 3414, Comment 27:

我在第3414期注释27找到了一个简单的解决方法:

An easy way to work around this problem is to call "setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);" on the listView views as they are added. You'll be able to select rows, click on rows and click on child checkboxes and buttons.

解决此问题的简单方法是调用“setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);”在listView视图中添加它们。您将能够选择行,单击行并单击子复选框和按钮。

It worked perfectly for me, although some casting was required:

它对我来说很完美,虽然需要一些铸件:

View v;
((ViewGroup)v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

#4


1  

You need to set a function setOnItemClickListener() and inside it declare something like this:

你需要设置一个函数setOnItemClickListener(),并在其中声明如下:

Uri uri = Uri.parse( "http://www.google.com" );
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );

#5


0  

The tricky part of listview is nothing within (for instance a TextView of a Button) is clickable!

listview中棘手的部分(例如Button的TextView)是可点击的!

Basically you need two string arrays:

基本上你需要两个字符串数组:

  1. names that users see on the list_view;
  2. 用户在list_view上看到的名称;

  3. hyperlinks that you want to direct them to go.
  4. 您要引导他们前往的超链接。

In the array.xml:

在array.xml中:

<string-array name="search_provider_name_array">
    <item>Google</item>
    <item>Yahoo</item>
    <item>Bing</item>
    <item>Baidu</item>
</string-array>
<string-array name="search_provider_link_array">
    <item>https://www.google.com</item>
    <item>https://www.yahoo.com</item>
    <item>https://www.bing.com</item>
    <item>https://www.baidu.com</item>
</string-array>

In the layout_search_provider.xml it contains a list view:

在layout_search_provider.xml中,它包含一个列表视图:

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lv_search"
    android:dividerHeight="1dp"/>

In your activity:

在您的活动中:

public class SearchProvider implements  AdapterView.OnItemClickListener {
    private ListView lv_search;
    private String[] names = getResources().getStringArray(R.array.search_provider_name_array);
    private String[] links = getResources().getStringArray(R.array.search_provider_link_array);

    //..

    @Override
    public View onCreateView(View v, String name, Context context, AttributeSet attrs) {
        lv_search= (ListView) v.findViewById(R.id.lv_search);

        ArrayAdapter sAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, names);
        lv_search.setAdapter(sAdapter);
        lv_search.setOnItemClickListener(this);

        return v;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if(i<links.length){
            Uri uri = Uri.parse(links[i]); 
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    }

}

When your list is dynamic, you can the following method(s) to update your listview.

当您的列表是动态列表时,您可以使用以下方法更新列表视图。

  • move the code in onCreateView() into onResume().
  • 将onCreateView()中的代码移动到onResume()。

  • sAdapter.notifyDataSetChanged();

#1


22  

This is done using the autoLink attribute of a TextView. Took me some time to dig through the documentation so putting it here with an example in case someone else is looking for it:

这是使用TextView的autoLink属性完成的。花了我一些时间来挖掘文档,所以把它放在这里,以防万一其他人正在寻找它:

Let us assume that you are binding your listview to a custom adapter. In that case, the following piece of code goes into your getView call:

我们假设您将listview绑定到自定义适配器。在这种情况下,以下代码段将进入getView调用:

Code:

textcontent.setText(Html.fromHtml(item.get_text()));
textcontent.setAutoLinkMask(Linkify.WEB_URLS);

Just put the link inside the text being passed to the setText call and you're done.

只需将链接放在要传递给setText调用的文本中,就可以了。

XML:

<TextView
                android:id="@+id/txtview"
                android:autoLink="web"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="put your link here"/>

Hope that helps...

希望有帮助......

#2


17  

If you have text that is already in HTML format, the best thing to do is the following:

如果您的文本已经是HTML格式,那么最好的办法是:

TextView textcontent = (TextView) findViewById(...);
textcontent.setMovementMethod(LinkMovementMethod.getInstance());

String text = "<a href="http://www.*.com">*.com</a>";
textcontent.setText(Html.fromHtml(text));

This will cause any link tags to be clickable in your text view. Alternately, you could use android:autoLink="web" as suggested by Legend, but this has the side-effect of a) linkifying urls that are not wrapped in anchor tags, and b) potentially missing urls or linkifying things that aren't urls. If you want the smarts of autoLink then you should use it, but if all you want is to linkify the tags that are already there, you're better off using setMovementMethod().

这将导致在文本视图中可以单击任何链接标记。或者,您可以按照Legend的建议使用android:autoLink =“web”,但这会产生以下副作用:a)链接未包含在锚标记中的网址,以及b)可能缺少网址或链接不是网址。如果你想要autoLink的智能,那么你应该使用它,但如果你想要的只是链接已经存在的标签,你最好使用setMovementMethod()。

See this bug report for more details: http://code.google.com/p/android/issues/detail?id=2219

有关详细信息,请参阅此错误报告:http://code.google.com/p/android/issues/detail?id = 2219

#3


10  

Hmm, it seems that adding textcontent.setMovementMethod(LinkMovementMethod.getInstance()); makes it so that the clicks on the textview's text parts are no longer passed through to the listview below.

嗯,似乎添加了textcontent.setMovementMethod(LinkMovementMethod.getInstance());这使得textview文本部分的点击不再传递到下面的列表视图。

I found a simple workaround under Issue 3414, Comment 27:

我在第3414期注释27找到了一个简单的解决方法:

An easy way to work around this problem is to call "setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);" on the listView views as they are added. You'll be able to select rows, click on rows and click on child checkboxes and buttons.

解决此问题的简单方法是调用“setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);”在listView视图中添加它们。您将能够选择行,单击行并单击子复选框和按钮。

It worked perfectly for me, although some casting was required:

它对我来说很完美,虽然需要一些铸件:

View v;
((ViewGroup)v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

#4


1  

You need to set a function setOnItemClickListener() and inside it declare something like this:

你需要设置一个函数setOnItemClickListener(),并在其中声明如下:

Uri uri = Uri.parse( "http://www.google.com" );
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );

#5


0  

The tricky part of listview is nothing within (for instance a TextView of a Button) is clickable!

listview中棘手的部分(例如Button的TextView)是可点击的!

Basically you need two string arrays:

基本上你需要两个字符串数组:

  1. names that users see on the list_view;
  2. 用户在list_view上看到的名称;

  3. hyperlinks that you want to direct them to go.
  4. 您要引导他们前往的超链接。

In the array.xml:

在array.xml中:

<string-array name="search_provider_name_array">
    <item>Google</item>
    <item>Yahoo</item>
    <item>Bing</item>
    <item>Baidu</item>
</string-array>
<string-array name="search_provider_link_array">
    <item>https://www.google.com</item>
    <item>https://www.yahoo.com</item>
    <item>https://www.bing.com</item>
    <item>https://www.baidu.com</item>
</string-array>

In the layout_search_provider.xml it contains a list view:

在layout_search_provider.xml中,它包含一个列表视图:

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lv_search"
    android:dividerHeight="1dp"/>

In your activity:

在您的活动中:

public class SearchProvider implements  AdapterView.OnItemClickListener {
    private ListView lv_search;
    private String[] names = getResources().getStringArray(R.array.search_provider_name_array);
    private String[] links = getResources().getStringArray(R.array.search_provider_link_array);

    //..

    @Override
    public View onCreateView(View v, String name, Context context, AttributeSet attrs) {
        lv_search= (ListView) v.findViewById(R.id.lv_search);

        ArrayAdapter sAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, names);
        lv_search.setAdapter(sAdapter);
        lv_search.setOnItemClickListener(this);

        return v;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if(i<links.length){
            Uri uri = Uri.parse(links[i]); 
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    }

}

When your list is dynamic, you can the following method(s) to update your listview.

当您的列表是动态列表时,您可以使用以下方法更新列表视图。

  • move the code in onCreateView() into onResume().
  • 将onCreateView()中的代码移动到onResume()。

  • sAdapter.notifyDataSetChanged();