如何为所有textview URL创建通用标记?(How to create general tag for all textview URL's?)

编程入门 行业动态 更新时间:2024-10-26 13:34:33
何为所有textview URL创建通用标记?(How to create general tag for all textview URL's?)

我有一个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.

更多推荐

本文发布于:2023-08-06 12:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448863.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   标记   textview   tag   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!