如何在textview android上设置多次单击

时间:2017-09-22 13:13:37

标签: android onclick textview onclicklistener

我想在textview上设置多次点击。我看到很多答案,但任何答案都无法帮助我。我使用以下代码创建跨区字符串: -

  private SpannableStringBuilder addClickablePart(String str) {
    SpannableStringBuilder ssb = new SpannableStringBuilder(Html.fromHtml(deafultSpna + feelingSpan+tagfriendspan));
    ssb.setSpan(new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            Toast.makeText(AddPostActivity.this, "hello click",
                    Toast.LENGTH_SHORT).show();
        }
    }, 0, Html.fromHtml(deafultSpna).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            Toast.makeText(AddPostActivity.this, "hello click ffff",
                    Toast.LENGTH_SHORT).show();
        }
    }, Html.fromHtml(deafultSpna).length(), Html.fromHtml(deafultSpna+feelingSpan).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            Toast.makeText(AddPostActivity.this, "hello click ffff fdsfds",
                    Toast.LENGTH_SHORT).show();
        }
    }, Html.fromHtml(deafultSpna+feelingSpan).length(), Html.fromHtml(deafultSpna+feelingSpan+tagfriendspan).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return ssb;
}

但使用此代码我发现这样的观点: - My layout look this

但我想这样: -

enter image description here

我使用此代码feelingSpan = "<font color=#414141> - Feeling </font><font color=#bd2436>" + feeling_name + "</font>";

进行跨越

任何人都可以帮助我解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:3)

为了简化操作,我们可以首先定义一个小类,使可点击的跨度变为红色并在单击时显示相应的消息。

private class MyClickableSpan extends ClickableSpan {
    String text;

    MyClickableSpan(String text) {
        this.text = text;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false); // get rid of underlining
        ds.setColor(Color.RED);     // make links red
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(view.getContext(), text, Toast.LENGTH_SHORT).show();
    }
}

接下来,我们可以添加一个小的便捷方法来创建可点击的跨度。

private void addClickableText(SpannableStringBuilder ssb, int startPos, String clickableText, String toastText) {
    ssb.append(clickableText);
    ssb.setSpan(new MyClickableSpan(toastText), startPos, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

最后,我们可以填充spannable字符串并显示它。

    TextView text = findViewById(R.id.text);

    SpannableStringBuilder ssb = new SpannableStringBuilder("");
    addClickableText(ssb, ssb.length(), "Abhishek Nagar", "hello click");
    ssb.append(" - Feeling ");
    addClickableText(ssb, ssb.length(), "Sad", "hello click ffff");
    ssb.append(" with ");
    addClickableText(ssb, ssb.length(), "Anand Jainb", "hello click ffff fdsfds");

    text.setMovementMethod(LinkMovementMethod.getInstance());   // make our spans selectable
    text.setText(ssb);

这就是它的全部内容!

screenshot

答案 1 :(得分:1)

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.txt1);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(addClickablePart(getString(R.string.amanda_bio2)));
    }


    private SpannableStringBuilder addClickablePart(String str) {
        SpannableStringBuilder ssb = new SpannableStringBuilder(str);

        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "span clicked 1", Toast.LENGTH_LONG).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.RED);
            }
        }, 200, 214, 0);

        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "span clicked 2", Toast.LENGTH_LONG).show();

            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.BLUE);
            }
        }, 241, 255, 0);
        return ssb;
    }
}

您甚至可以添加多个跨度并自定义其颜色。