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

rjee0c15  于 2023-01-19  发布在  Android
关注(0)|答案(3)|浏览(181)

我想设置多点击文本视图。我看到许多答案,但任何答案不能帮助我。我用这个代码跨越字符串:-

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;
}

但是使用这段代码,我发现类似于这个视图:-x1c 0d1x
但我想喜欢这个:-

我使用代码feelingSpan = "<font color=#414141> - Feeling </font><font color=#bd2436>" + feeling_name + "</font>";创建了span
任何人都可以帮我解决这个问题。先谢了。

nqwrtyyt

nqwrtyyt1#

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

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);
}

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

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);

这就是它的全部!

6yjfywim

6yjfywim2#

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;
    }
}

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

hyrbngr7

hyrbngr73#

我已经为此目的创建了一个通用函数,希望这能有所帮助

fun TextView.makeLinks(vararg links: Triple<String, View.OnClickListener,LinkProperties>) {

    try {
        val spannableString = SpannableString(this.text)
        for (link in links) {
            val clickableSpan = object : ClickableSpan() {
                override fun updateDrawState(textPaint: TextPaint) {
                    super.updateDrawState(textPaint)
                    textPaint.isUnderlineText = link.third.isUnderlineText
                    textPaint.isFakeBoldText = link.third.isBoldText
                    textPaint.color = link.third.color ?: Color.parseColor(DCColorPicker.BLACK)
                }

                override fun onClick(view: View) {
                    Selection.setSelection((view as TextView).text as Spannable, 0)
                    view.invalidate()
                    link.second.onClick(view)
                }
            }
            val startIndexOfLink = this.text.toString().indexOf(link.first)
            spannableString.setSpan(
                clickableSpan, startIndexOfLink, startIndexOfLink + link.first.length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
        this.movementMethod = LinkMovementMethod.getInstance() // without LinkMovementMethod, link can not click
        this.setText(spannableString, TextView.BufferType.SPANNABLE)
    } catch (e: Exception) {
        Log.e(TAG, "makeLinks: " + e.message)
    }
}

您可以在此处添加N个属性

data class LinkProperties(val isUnderlineText: Boolean = false, val isBoldText: Boolean = false, val color: Int?=null)

对于调用,您可以像这样使用此函数

val linkBoldOnly = LinkProperties(isBoldText = true)
val linkBoldUnderline = LinkProperties(isUnderlineText = true, isBoldText = true)

binding.name.makeLinks(
Triple("text", View.OnClickListener { openMore() }, linkBoldUnderline),
Triple(name.toString(), View.OnClickListener { itemClick() }, linkBoldOnly)
)

相关问题