如何为所有文本视图 URL 创建通用标签?

How to create general tag for all textview URL's?

我有一个文本视图,其中包含一个亚马逊URL。我可以使用完整的 URL 来完成这项工作,它会在浏览器中打开 link。

但是,我不想在文本视图中显示整个 URL,我想用文本 'Buy' 替换它。我希望将所有 link 的 textview 设置为 BUY。

我看到每个 textview 和 URL 都是单独修复的问题。但并非普遍适用于所有人。我尝试了这个 - http://jtomlinson.blogspot.co.uk/2010/03/textview-and-html.html - 我设法将文本设置为 'Buy' 但它不再是可点击的 link.

只是为了提供更多信息。在我的应用程序中,我将 xml 解析到数据库中,搜索将 return 生成包含文本视图的列表视图。

这是我的 onclicklistener 代码:

 myList.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                // Get the cursor, positioned to the corresponding row in the result set
                Cursor cursor = (Cursor) myList.getItemAtPosition(position);

                //fix this line. modify string value
                // String searchValue = cursor.getString(cursor.getColumnIndexOrThrow("searchValue"));
                String author = cursor.getString(cursor.getColumnIndexOrThrow("author"));
                String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
                String price = cursor.getString(cursor.getColumnIndexOrThrow("price"));
                String publish_date = cursor.getString(cursor.getColumnIndexOrThrow("date"));
                String description = cursor.getString(cursor.getColumnIndexOrThrow("description"));
                String module = cursor.getString(cursor.getColumnIndexOrThrow("module"));
                String buy = cursor.getString(cursor.getColumnIndexOrThrow("buy"));

                //Check if the Layout already exists
                LinearLayout bookLayout = (LinearLayout)findViewById(R.id.customerLayout);
                if(bookLayout == null){
                    //Inflate the Customer Information Vie
                    LinearLayout xbookLayout = (LinearLayout)findViewById(R.id.Layout);

                    View book = getLayoutInflater().inflate(R.layout.book_info, xbookLayout, false);
                    xbookLayout.addView(book);
                }

                //Get References to the TextViews
                authorText = (TextView) findViewById(R.id.xauthor);
                titleText = (TextView) findViewById(R.id.xtitle);
                priceText = (TextView) findViewById(R.id.xprice);
                publishDateText = (TextView) findViewById(R.id.xpublish_date);
                descriptionText = (TextView) findViewById(R.id.xdescription);
                moduleText = (TextView) findViewById(R.id.xmodule);
                buyText = (TextView) findViewById(R.id.xbuy);

                // Update the parent class's TextView
                authorText.setText(author);
                titleText.setText(title);
                priceText.setText(price);
                publishDateText.setText(publish_date);
                descriptionText.setText(description);
                moduleText.setText(module);
                buyText.setText(buy);


                searchView.setQuery("",true);

您错过了将 setMovementMethod(LinkMovementMethod.getInstance()) 设置为 textview,没有它 textview 将无法像

一样打开 link.Do
    TextView tv =(TextView) findViewById(R.id.textView1);
    String url ="http://jtomlinson.blogspot.co.uk/2010/03/textview-and-html.html";
    String htmltext ="<a href ="+url+">BUY</a>";
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    tv.setText(Html.fromHtml(htmltext));

注意:请记住,您拥有互联网许可。