为textview中的文本添加不同的侦听器

qmb5sa22  于 2021-07-09  发布在  Java
关注(0)|答案(4)|浏览(368)

我有一个文本视图,其中设置了一些文本:

lblText.setText("Hello How Are You");

现在我想用以下方式:
每当我点击hello,一个 Toast 应该显示“你好”。每当我点击“如何”时 Toast 应该显示“如何”。
等等。
如何检查我点击了哪个单词,并对一个单词中的不同单词执行不同的操作 TextView

mxg2im7a

mxg2im7a1#

试试这个:

TextView tv = (TextView)findViewById(R.id.textView);      
    Spannable span = Spannable.Factory.getInstance().newSpannable("Hello How Are You");   
span.setSpan(new ClickableSpan() {  
    @Override
    public void onClick(View v) {  
        Log.d("main", "Helo clicked");
        Toast.makeText(MainActivity.this, "hello clicked", Toast.LENGTH_SHORT).show(); 
    } }, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new ClickableSpan() {  
   @Override
   public void onClick(View v) {  
       Log.d("main", "how clicked");
       Toast.makeText(MainActivity.this, "how clicked", Toast.LENGTH_SHORT).show(); 
   } }, 6, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(span); 
tv.setMovementMethod(LinkMovementMethod.getInstance());
x8goxv8g

x8goxv8g2#

这是一个有效的例子。理解它并根据需要在代码中应用它。

SpannableString ss = new SpannableString("Hello How Are You");
    ClickableSpan clickableSpan1 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
           //toast hello
        }
    };
    ClickableSpan clickableSpan2 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
          // toast how
        }
    };
    ClickableSpan clickableSpan3 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
           //toast are
        }
    };
    ClickableSpan clickableSpan4 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
           //toast you
        }
    };
    tView.setText(ss);
    ss.setSpan(clickableSpan1, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(clickableSpan2, 5, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(clickableSpan3, 9, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(clickableSpan4, 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ctzwtxfj

ctzwtxfj3#

你必须使用 SpannableString 如果不想超过1 TextView .
编辑:
对于每个字符串,您都可以注册ClickablesEnable,例如:

private class MyClickableSpannable extends ClickableSpan {

    private final String mStringToShow;

    public MyClickableSpannable(String stringToShow) {
        mStringToShow = stringToShow;
    } 

    @Override
    public void onClick(View widget) {
         Toast.makeText(context, mStringToShow, Toast.LENGHT_SHORT).show();
    }
}

设置侦听器:

String myString = "Hello How Are You"; 
String hello = "Hello";
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(myString);
spannableStringBuilder.setSpan(
   new MyClickableSpannable(hello), startIndexOfHello,
   startIndexOfHello + hello.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
cl25kdpy

cl25kdpy4#

试试这个。这对我很有效。

SpannableString SpanString = new SpannableString(
            "Hello How Are You");

    ClickableSpan hello = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

        Toast.makeText(this, "Hello is clicked", Toast.LENGTH_SHORT).show();
        }
    };

    ClickableSpan how = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

          Toast.makeText(this, "how is clicked", Toast.LENGTH_SHORT).show();

        }
    };

    SpanString.setSpan(hello, 0, 6, 0);
    SpanString.setSpan(how, 6, 9, 0);

    lblText.setMovementMethod(LinkMovementMethod.getInstance());
    lblText.setText(SpanString, BufferType.SPANNABLE);

相关问题