Android set autoLink attribute programmatically

↘锁芯ラ 提交于 2019-12-19 14:26:09

问题


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

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

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

tw_phone.setAutoLinkMask(0x04);

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.

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);

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.


回答1:


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

where "contatti" has value +39012345678 with international prefix



来源:https://stackoverflow.com/questions/33258635/android-set-autolink-attribute-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!