clickable word inside TextView in android

后端 未结 6 2048
谎友^
谎友^ 2020-12-04 19:36

I have TextView with text that changed dynamically. This text contain strings like myWord. I want that after click to this \"

6条回答
  •  旧巷少年郎
    2020-12-04 20:25

    Better approach is

       SpannableString ss = new SpannableString("Android is a Software stack");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                startActivity(new Intent(MyActivity.this, NextActivity.class));
            }
        };
        ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    //where 22 and 27 are the starting and ending index of the String. Now word stack is clickable 
    // onClicking stack it will open NextActiivty
    
        TextView textView = (TextView) findViewById(R.id.hello);
        textView.setText(ss);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    

提交回复
热议问题