【问题标题】:How to create general tag for all textview URL's?如何为所有 textview URL 创建通用标签?
【发布时间】:2015-06-13 06:47:31
【问题描述】:

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

但是,我不想在 textview 中显示整个 URL,我想将其替换为文本“购买”。我希望将所有链接的 textview 设置为 BUY。

我看到了每个文本视图和 URL 都是单独固定的问题。但一般来说并不是所有人。我试过这个 - http://jtomlinson.blogspot.co.uk/2010/03/textview-and-html.html - 我设法将文本设置为“购买”,但它不再是一个可点击的链接。

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

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

【问题讨论】:

  • 如果你有项目在列表中的位置(在 OnItemClick 中)然后只需调用一个光标和 getString 再次“购买”......就像你在 getView 中所做的那样。最好叫它“url”。
  • 然后呢?不可能,目前我的链接可以通过xml中的这段代码点击 android:clickable="true" android:autoLink="all"/>
  • 您不需要链接。如果您在列表视图上设置了项目点击侦听器,则您的文本视图是可点击的。因此,如果单击该项目,则提取相应的 url 并移交给浏览器。
  • 对不起,我只是不明白你想说什么。我可以点击链接并正常工作。但这仅来自存储在我的数据库中的纯 URL。我想用文本“购买”替换将放置在 buyText 文本视图中的每个 URL。
  • 您可以在您想要的每个文本视图中放置“购买”。该文本无关紧要。在点击处理程序中,您可以获得位置,并且可以获得该位置的 url。然后启动浏览器。我没有看到你的问题。

标签: android html url hyperlink textview


【解决方案1】:

您错过了将 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));

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

【讨论】:

  • 这确实有道理。虽然我不认为它完全适用于我想要的。在您的代码中,您有 String url。但是,我不想为每个 textview 输入字符串 URL。我有一个包含大量不同 url 的 XML 文件,它们出现在我的 textview 中。我想要做的是,无论 URL 是什么,都将 buyText 文本视图始终设置为购买。我希望这是有道理的
  • 进一步添加。基本上,buyText 文本视图在我的列表视图中出现了多次。 URL 都是从我的数据库中检索到的。我希望每个 URL 都放在 buyText texview 中,并且我希望 textview 文本是“购买”。
  • 嗯,这正是你在这里得到的。 (如果有效)。
  • 您应该在列表适配器中的 getView() 方法中将 url 绑定到 textview,而不是在列表项单击侦听器中。
猜你喜欢
  • 2016-01-04
  • 2011-06-26
  • 2012-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多