如何在TextView中制作几个可单击的文本部分

时间:2013-10-07 14:20:03

标签: android textview

如何在TextView中制作几个可单击的文本部分。每个可点击的部分都必须有自己的行动。

3 个答案:

答案 0 :(得分:54)

您可以使用android.text.style.ClickableSpan

    SpannableString ss = new SpannableString("Hello World");
    ClickableSpan span1 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            // do some thing
        }
    };

    ClickableSpan span2 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            // do another thing
        }
    };

    ss.setSpan(span1, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(span2, 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(ss);
    textView.setMovementMethod(LinkMovementMethod.getInstance());

答案 1 :(得分:3)

我将主要文字和可点击的字词存储在不同的资源中。

某些配置中不存在资源中的字符串。

    String[] links = new String[3];

    links[0] = cntx.getString(cntx.getResources().getIdentifier("footerLink1", "string", cntx.getPackageName()));
    links[1] = cntx.getString(cntx.getResources().getIdentifier("footerLink2", "string", cntx.getPackageName()));
    links[2] = cntx.getString(cntx.getResources().getIdentifier("footerLink3", "string", cntx.getPackageName()));
    String text = String.format(cntx.getString(cntx.getResources().getIdentifier("footerDisclaimer", "string", cntx.getPackageName())), links[0], links[1], links[2]);
    SpannableString ss = new SpannableString(text);
    setSpanOnLink(ss, links[0], new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Log.i("Disclaimer Footer", "1 click");
            //TODO run item 
        }
    });
    setSpanOnLink(ss, links[1], new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Log.i("Disclaimer Footer", "2 click");
            //TODO run item 
        }
    });
    setSpanOnLink(ss, links[2], new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Log.i("Disclaimer Footer", "3click");
            //TODO run item 
        }
    });

    TextView t1 = new TextView(cntx);
    t1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 8);
    t1.setText(ss);
    t1.setMovementMethod(LinkMovementMethod.getInstance());

private void setSpanOnLink(SpannableString ss, String link, ClickableSpan cs) {
        String text = ss.toString();
        int start = text.indexOf(link);
        int end = start + link.length();
        ss.setSpan(cs, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

答案 2 :(得分:-1)

这对我有用:

xml中的

    <TextView
        android:id="@+id/tv_by_continuing_str"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:textSize="15sp"
        tools:text="Test msg 1 2, 3"
        android:textColor="@color/translucent_less_white3"
        android:textColorLink="@color/white"
        android:gravity="center|bottom"
        android:layout_above="@+id/btn_privacy_continue" />

在strings.xml中

< string name="by_continuing_str2">< ! [ CDATA[By continuing to use this app, you agree to our <a href="https://go.test.com" style="color:gray"/> Privacy Statement </a> and <a href="https://go.test.com" style="color:gray"/>Services Agreement.]]>< / string>
活动中的

TextView tv_by_continuing = (TextView) findViewById(R.id.tv_by_continuing);
tv_by_continuing.setText(Html.fromHtml(getString(R.string.by_continuing_str2)));
tv_by_continuing.setMovementMethod(LinkMovementMethod.getInstance());