Multiple Clickable links in TextView on Android

后端 未结 8 738
无人共我
无人共我 2020-12-14 07:16

I\'m trying to add multiple links in a textview similar to what Google & Flipboard has done below with their Terms and conditions AND Privacy Po

8条回答
  •  感情败类
    2020-12-14 07:51

    Here's my solution:

    First we need to have clickable links in our TextView:

    1. Here's my TextView in the xml layout, do not add any links handling parameters.

      
      
    2. In strings file, I added the resource text with html tags

      By signing up you agree to our Terms of Service and Privacy Policy.
      
    3. In onCreateView set LinkMovementMethod to the TextView

      TextView privacyTextView = (TextView) root.findViewById(R.id.sign_up_privacy);
      privacyTextView.setMovementMethod(LinkMovementMethod.getInstance());
      

    Now the TextView links are clickable.

    Second, We need to Handle these clicks:

    1. In my Manifest file, I added intent-filter for "terms" and "privacy" and Single Instance Launch Mode

      
                  
                      
                      
      
                      
                      
                  
              
      
    2. In MyActivity, override onNewIntent to catch privacy and terms intents

      @Override
          protected void onNewIntent(Intent intent)
          {
              if (intent.getScheme().equalsIgnoreCase(getString("terms")))
              {
                  //handle terms clicked
              }
              else if (intent.getScheme().equalsIgnoreCase(getString("privacy")))
              {
                  //handle privacy clicked
              }
              else
              {
                  super.onNewIntent(intent);
              }
          }
      

提交回复
热议问题