Android以编程方式设置autoLink属性 [英] Android set autoLink attribute programmatically

查看:439
本文介绍了Android以编程方式设置autoLink属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<TextView
    android:text="123456789"
    android:autoLink="phone">
</TextView>

我想通过代码创建此TextView,但是我遇到了无数问题.

I want to create this TextView from code, however I am encountering countless problems.

首先,我在创建TextView并将其添加到一半的过程中:

In the first place, I got halfway creating a TextView and adding this:

tw_phone.setAutoLinkMask(0x04);

这导致可单击的TextView,但是单击时,吐司说没有应用程序可以执行此操作"或类似的命令.我也尝试过

This resulted in a clickable TextView, but when you clicked, a toast said "No application can perform this action", or something similar. I also tried with

Linkify.addLinks(tw_phone, Linkify.PHONE_NUMBERS); //and .ALL

但是它给了我相同的结果.

but it gave me the same result.

当我决定在StackOverflow上提问时,我尝试将代码精简化,以防使用布局的方式出现问题(您永远不知道),但是现在我什至无法使TextView可点击.我认为这是应该起作用的代码,因为它只是我在代码中更深入使用的精简版本.

When I decided to ask on StackOverflow, I tried to strip my code down incase there were something wrong with the way I have used Layouts (you never know), but now I'm not even able to make a TextView clickable. This is the code that, in my opinion, should work as it is just a stripped down version of what I am using deeper in my code.

TableLayout table = (TableLayout) findViewById(R.id.tableResult);
TableRow row = new TableRow(this);
TextView tw = new TextView(this);
tw.setText("123456789");
tw.setAutoLinkMask(Linkify.ALL);
row.addView(tw);
table.addView(row);

有人可以写一个简单的小例子来说明如何创建TextView,给它一个数字作为文本,然后允许用户单击它并选择他们想用来打开数字的任何应用程序吗? 如果您也可以指出我的代码出了什么问题,那将是很好的,但我宁愿直接获得答案.我尝试过的东西来自其他StackOverflow问题和答案.

Can someone write a simple, small example of how you create a TextView, give it a number as text and then allows the user to click on it and choose whatever app they want to open the number with?? If you can point out whats wrong in my code aswell, that would be great, but I would much rather just get the answer straight away. The things I have tried are taken from other StackOverflow questions and answers.

推荐答案

TextView tv_contatti2 = new TextView(this); tv_contatti2.setText(contatti);
Linkify.addLinks(tv_contatti2, Linkify.PHONE_NUMBERS);
tv_contatti2.setLinksClickable(true);

其中"contatti"的值为+39012345678,带有国际前缀

where "contatti" has value +39012345678 with international prefix

这篇关于Android以编程方式设置autoLink属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆