我有一个textview,其中包含一个亚马逊网址。 我可以使用完整的URL来完成这项工作,它将在浏览器中打开链接。
但是,我不想在textview中使用整个URL,我想用文本'Buy'替换它。 我希望将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);I have a textview, which contains an amazon URL. I am able to make this work with the full URL, and it will open the link in the browser.
However, I do not want the entire URL in the textview, I want to replace it with the text 'Buy'. I want the textview to be set to BUY for all links.
I have seen questions where each textview and URL are fixed individually. But not generally for all. I tried this - http://jtomlinson.blogspot.co.uk/2010/03/textview-and-html.html - and I managed to set the text to 'Buy' but it was no longer a a clickable link.
Just to give some more info. In my app, I parse an xml into a database, and a search will return results in a listview containing textviews.
Here is my code for the 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将无法打开链接。它喜欢它
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));注意:请记住,您拥有互联网权限。
You missed to set the setMovementMethod(LinkMovementMethod.getInstance()) to textview,without it textview will not be able to open the link.Do it like
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));Note : Keep in mind that you have internet permission.
更多推荐
发布评论