handle textview link click in my android app

前端 未结 12 911
迷失自我
迷失自我 2020-11-22 04:33

I\'m currently rendering HTML input in a TextView like so:

tv.setText(Html.fromHtml(\"test\"));

The HTML b

12条回答
  •  长情又很酷
    2020-11-22 04:59

    Example: Suppose you have set some text in textview and you want to provide a link on a particular text expression: "Click on #facebook will take you to facebook.com"

    In layout xml:

    
    

    In Activity:

    String text  =  "Click on #facebook will take you to facebook.com";
    tv.setText(text);
    Pattern tagMatcher = Pattern.compile("[#]+[A-Za-z0-9-_]+\\b");
    String newActivityURL = "content://ankit.testactivity/";
    Linkify.addLinks(tv, tagMatcher, newActivityURL);
    

    Also create one tag provider as:

    public class TagProvider extends ContentProvider {
    
        @Override
        public int delete(Uri arg0, String arg1, String[] arg2) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public String getType(Uri arg0) {
            return "vnd.android.cursor.item/vnd.cc.tag";
        }
    
        @Override
        public Uri insert(Uri arg0, ContentValues arg1) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public boolean onCreate() {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
                            String arg4) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
            // TODO Auto-generated method stub
            return 0;
        }
    
    }
    

    In manifest file make as entry for provider and test activity as:

    
    
    
        
            
            
            
        
    
    

    Now when you click on #facebook, it will invoke testactivtiy. And in test activity you can get the data as:

    Uri uri = getIntent().getData();
    

提交回复
热议问题