gpt4 book ai didi

android - 通过 TextView 打开网页

转载 作者:行者123 更新时间:2023-11-30 02:28:36 24 4
gpt4 key购买 nike

我定义了以下 TextView:

<TextView
android:id="@+id/card12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_margin="15dp"
android:padding="15dp"
android:background="@drawable/selector_card_background"
android:text="Go to Google"/>

我想在 c点击 TextView 时打开一个 webpage(它显示一个 text 而不是完整的 网址)

我已经阅读了很多关于此的内容,但是我尝试过的所有方法都不适合我。我试过编写 android:autoLink="web"view.setMovementMethod(LinkMovementMethod.getInstance()); 但这不会使 TextView 可点击。我也尝试通过 intent 但为此我必须使用 Button 而不是 TextView

另外说一下我用的不是Activity,我用的是Fragment:

public class InformationFragment extends Fragment {

public InformationFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.information_layout, container, false);

return rootView;
}
}

关于如何做到这一点有什么想法吗?非常感谢。

最佳答案

试试这个:

TextView link = (TextView)findViewById(R.id.card12);
link.setText(Html.fromHtml("<a href=\"http://google.com\">Go to Google</a>"));
link.setMovementMethod(LinkMovementMethod.getInstance());

关于android - 通过 TextView 打开网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27588995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com