Create clickable link in text view in android

后端 未结 2 764
Happy的楠姐
Happy的楠姐 2020-11-29 09:55

I have some hash tags in a TextView which are starts with \'#\'

Example:\"#one#two Hello World #three\".

I wants these hash tags clickable separ

2条回答
  •  忘掉有多难
    2020-11-29 10:45

    Modify the below according to your requirement. Use a SpannableString

    String s ="#one #Two Hello World #three";
    String split[] = s.split("#");
    TextView_tv = (TextView) findViewById( R.id.tv );
    for(int i=1;i

    Snap on Emulator

    On each hash click displays toast one, two and three. Instead of toast start a new activity.

    enter image description here

    Edit:

    If you want the string clicked

     ss1.setSpan(new  MyClickableSpan(""+i,split[i]), 0, 1,  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    

    Then

     String clicked;
     String astring;
     public MyClickableSpan(String check,String actualstring) {
    super();
    clicked =check;
    astring =actualstring; // pass this to next activity using intent
    }
    

    Then

      public void onClick(View tv) {
            if(clicked.equals("1"))
            {
                 Toast.makeText(getApplicationContext(), astring,1000).show();
            }
            else if(clicked.equals("2"))
            {
                Toast.makeText(getApplicationContext(), astring,1000).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), astring,1000).show();       
            }
    
       }
    

提交回复
热议问题