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

时间:2014-03-11 12:16:48

标签: java android textview

我有一个TextView,我在其中设置了一些文字:

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

现在我想用以下方式:

每当我点击Hello时,都会显示Toast,说“你好”。 每当我点击“如何”时,都应显示Toast,说“如何”。

等等。

如何查看我点击的单词,并为TextView

中的不同单词执行不同的操作

4 个答案:

答案 0 :(得分:3)

如果您不想拥有超过1 SpannableString,则必须使用TextView

编辑:

对于每个String,您可以注册ClickableSpannable,例如:

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

答案 1 :(得分:1)

这是一个有效的例子。了解它并根据需要将其应用于您的代码中。

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

答案 2 :(得分:0)

试一试。它非常适合我。

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

答案 3 :(得分:0)

试试这个:

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