How to set the part of the text view is clickable

后端 未结 20 990
无人共我
无人共我 2020-11-22 01:29

I have the text \"Android is a Software stack\". In this text i want to set the \"stack\" text is clickable. in the sense if you click on t

20条回答
  •  长情又很酷
    2020-11-22 02:15

    I would suggest a different approach that I think requires less code and is more "localization-friendly".

    Supposing that your destination activity is called "ActivityStack", define in the manifest an intent filter for it with a custom scheme (e.g. "myappscheme") in AndroidManifest.xml:

    
        
            
            
            
            
        
    
    

    Define the TextView without any special tag (it is important to NOT use the "android:autoLink" tag, see: https://stackoverflow.com/a/20647011/1699702):

    
    

    then use a link with custom scheme and host in the text of the TextView as (in String.xml):

    Android is a Software stack
    

    and "activate" the link with setMovementMethod() (in onCreate() for activities or onCreateView() for fragments):

    TextView stack = findViewById(R.id.stackView);
    stack.setMovementMethod(LinkMovementMethod.getInstance());
    

    This will open the stack activity with a tap on the "stack" word.

提交回复
热议问题