简体   繁体   English

如何在Android中将TextView设置为链接? 我不想显示链接,但显示诸如“单击我”之类的其他内容

[英]How to set a TextView in Android as a link? I don't want to display the link but something else like “click me”

我试图在我的Android应用程序登录页面上添加一个URL,该URL重定向用户以恢复密码。

I guess you want to show the user a text looking like a url so that when the user taps on it you can redirect him to a web page. 我猜您想向用户显示一个类似于url的文本,以便当用户点击它时可以将其重定向到网页。 In your xml layout, declare a TextView with text attribute set 在您的xml布局中,声明一个具有text属性集的TextView

<TextView android:id = "@+id/txt"
...

android:text= "Click me !">

And in your activity class, 在您的活动课中

txt = (TextView)findViewById(R.id.txt);
SpannableString content = new SpannableString(txt.getText());
content.setSpan(new UnderlineSpan(), 0, txt.length(), 0);
txt.setText(content);
txt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse("http://www.google.com"));
        startActivity(i);
    }
});

You are thinking in terms of how a web page would do it, but it doesn't necessarily have to be a text link like a web page. 您在考虑网页的操作方式,但是不一定像网页一样是文本链接。 You could simply have a button (or any view really) that takes the user to password recovery when touched. 您可以简单地拥有一个按钮(或任何视图),使用户在触摸时可以恢复密码。

As an example, this is how you would do it with a button in your layout: 例如,这是您在布局中使用按钮的方式:

Button button = (Button) findViewById(R.id.your_button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String url = "http://www.your-password-recovery-page.com";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
});

This will launch the default browser of the device and navigate to your password recovery url. 这将启动设备的默认浏览器,并导航到您的密码恢复URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM