Java/Android - 将 link 隐藏为文本

Java/Android - Hide a link as text

大家好,我一直在努力解决这个问题,并查看了这里的许多问题,但似乎找不到我的问题的答案。我正在制作一个从数组中随机显示晚餐的应用程序。我希望这些晚餐可以点击并将用户带到网页,但我不知道如何实现这一点,所以目前我刚刚在晚餐下面添加了 link,看起来很丑。

这是包含食谱的class:

package me.oak.dinnertime;

import java.util.Random;

public class CookBook {
    public String[] mfood =
            {
                    "Chicago Deep Dish Pizza \n \n http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes",
                    "Spaghetti Bolognese \n \n http://www.bbcgoodfood.com/recipes/1502640/the-best-spaghetti-bolognese",
                    "Bourbon Chicken \n \n http://www.food.com/recipe/bourbon-chicken-45809",
            };


    public String getFood() {


        
        String food = "";
        //Randomly select a dinner
        Random randomGenerator = new Random();  //Construct a new Random number generator
        int randomNumber = randomGenerator.nextInt(mfood.length);
        //Convert random number to text

        food = mfood[randomNumber];


        return food;
    }
}

这里是主要的 activity:

package me.oak.dinnertime;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;



public class DinnerTimeActivity extends Activity {

    private CookBook mCookBook = new CookBook();
    private ColourWheel mColourWheel = new ColourWheel();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dinner_time);
        
        
        final TextView dinnerLabel = (TextView) findViewById(R.id.DinnerTextView);
        final Button showDinnerButton = (Button) findViewById(R.id.showDinnerButton);
        final RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.relativeLayout);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String food = mCookBook.getFood();
                //Update the label with the dinner
                dinnerLabel.setText(food);

                int colour = mColourWheel.getColour();
                relativelayout.setBackgroundColor(colour);
                showDinnerButton.setTextColor(colour);
            }
        };
        showDinnerButton.setOnClickListener(listener);

    }
}

这里是 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".DinnerTimeActivity"
    android:background="#ff51b46d"
    android:id="@+id/relativeLayout">


    <TextView android:text="What&apos;s for dinner?" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#80ffffff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/DinnerTextView"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="24sp"
        android:textColor="@android:color/white"
        android:text="Click the button to find out!"
        android:autoLink="web" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dinner Time"
        android:id="@+id/showDinnerButton"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white"
        android:textColor="#ff51b46d" />

</RelativeLayout>

不好意思说了这么多,希望有人能帮帮我。

我在 Hackforums 上为您回答了这个问题。

将链接存储在单独的数组中,确保链接的索引与另一个数组中的位置对齐。所以:

public class CookBook {
    public String[] mfood =
      {
      "Chicago Deep Dish Pizza",
      "Spaghetti Bolognese",
      "Bourbon Chicken",
      };
    public String[] mLinks =
      {
      "http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes",
      "http://www.bbcgoodfood.com/recipes/1502640/the-best-spaghetti-bolognese",
      "http://www.food.com/recipe/bourbon-chicken-45809",
      };

    public int getRandomFoodIndex() {
      //Randomly select a dinner
      Random randomGenerator = new Random();  //Construct a new Random number generator
      int randomNumber = randomGenerator.nextInt(mfood.length);
      //Convert random number to text

      return randomNumber;
    }

    public String getFood(int index) {
      return mfood[index];
    }

    public String getLink(int index) {
      return mLinks[index];
    }

}

然后

public class DinnerTimeActivity extends Activity {

    private CookBook mCookBook = new CookBook();
    private ColourWheel mColourWheel = new ColourWheel();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_dinner_time);

      final TextView dinnerLabel = (TextView) findViewById(R.id.DinnerTextView);
      final Button showDinnerButton = (Button) findViewById(R.id.showDinnerButton);
      final RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.relativeLayout);

      View.OnClickListener listener = new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              int index = mCookBook.getRandomFoodIndex();
              String food = mCookBook.getFood(index);
              String link = mCookBook.getLink(index);
              //Update the label with the dinner
              dinnerLabel.setText(food);

              /** open link with the link variable */

              int colour = mColourWheel.getColour();
              relativelayout.setBackgroundColor(colour);
          showDinnerButton.setTextColor(colour);
          }
      };
      showDinnerButton.setOnClickListener(listener);
    }
}

要使用 LinkMovementMethod,请尝试以下操作:

更改您的数组列表内容:

Chicago Deep Dish Pizza \n \n http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes

至:

<a href=\"http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes\">Chicago Deep Dish Pizza</a>

当将此文本设置到您的 TextView 时,请按以下方式操作:

(更新:删除下划线并更改文本颜色,来源:Remove underline from links in TextView - Android

Spannable s = (Spannable) Html.fromHtml(foodString);
for (URLSpan u: s.getSpans(0, s.length(), URLSpan.class)) {
     s.setSpan(new UnderlineSpan() {
         public void updateDrawState(TextPaint tp) {
              //remove the underline
              tp.setUnderlineText(false);       
              //set text color            
              tp.setColor(getResources().getColor(R.color.orange));
          }
      }, s.getSpanStart(u), s.getSpanEnd(u), 0);
 }
 dinnerLabel.setText(s);
 dinnerLabel.setMovementMethod(LinkMovementMethod.getInstance());

同时删除

android:autoLink="web"

在 xml.

正如我所测试的,"Chicago Deep Dish Pizza" 将在测试视图中显示为可点击的 link。

只需在您的字符串文件中使用类似的内容并在文本视图中引用它 <string name="links"><a href="www.google.com\">Google</a> shalom is a boy<a href="www.google.com\">Google 2 </a> </string> 你也可以在字符串文件的数组中使用它来让它真正工作做这个 terms = findViewById(R.id.terms); terms.setMovementMethod(LinkMovementMethod.getInstance());