如何为所有textview URL创建通用标记?

时间:2015-04-08 10:43:33

标签: android html url hyperlink textview

我有一个textview,其中包含一个亚马逊网址。我可以使用完整的URL进行此操作,它将在浏览器中打开链接。

但是,我不想在textview中使用整个网址,我想将其替换为“购买”文字。我希望将textview设置为所有链接的BUY。

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

只是提供更多信息。在我的应用程序中,我将xml解析为数据库,搜索将在包含textviews的listview中返回结果。

这是我的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);

1 个答案:

答案 0 :(得分:0)

您错过了将setMovementMethod(LinkMovementMethod.getInstance())设置为textview,没有它textview将无法打开链接。它喜欢

    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));

注意:请记住,您拥有互联网权限。