首页 > 解决方案 > txt文件中的可点击单词

问题描述

我使用以下代码从 textview 中的文件夹中读取了一个 .txt 文件:

    String txt = "";
        StringBuffer sbuffer1 = new StringBuffer();
        InputStream is = this.getResources().openRawResource(R.raw.file);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        try {

            while ((txt = reader.readLine()) !=null){
                sbuffer1.append(txt + "\n");

            }
            textview.setText(sbuffer1);
            is.close();

        }catch(Exception e) {
            e.printStackTrace();
     }

.txt 文件包含很长的文本,我希望文本中的特定单词可以点击,当点击特定单词时,我想显示一条 toast 消息。我怎样才能做到这一点?

这是 spannablestring 的示例代码,但我不知道如何在我的代码案例中应用它:

    String text = " what do I put here??";
    SpannableString ss = new SpannableString(text);
    ClickableSpan clickableSpan1 = new ClickableSpan() {
           @Override
           public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "One", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {

                super.updateDrawState(ds);
                ds.setColor(Color.BLUE);
                ds.setUnderlineText(false);
            }
        };

        ClickableSpan clickableSpan2 = new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "Two", Toast.LENGTH_SHORT).show();
            }
         };

        ss.setSpan(clickableSpan1, 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(clickableSpan2, 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

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

标签: androidstringclickablespannablestring

解决方案


尝试这个:

    String txt = "";

    StringBuilder sbuffer1 = new StringBuilder();
    InputStream is = this.getResources().openRawResource(R.raw.file);
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));

    try {

        while ((txt = reader.readLine()) !=null){
            sbuffer1.append(txt).append("\n");
        }

        is.close();
    }catch(Exception e) {
        e.printStackTrace();
    }

    String text = sbuffer1.toString();
    SpannableStringBuilder builder = new SpannableStringBuilder(text);

    ClickableSpan clickable0 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Toast.makeText(MainActivity.this, "word0", Toast.LENGTH_LONG).show();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(Color.BLUE);
            ds.setUnderlineText(true);
        }
    };

    ClickableSpan clickable1 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Toast.makeText(MainActivity.this, "word1", Toast.LENGTH_LONG).show();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(Color.BLUE);
            ds.setUnderlineText(true);
        }
    };

    String word0 = "word0";
    String word1 = "word1";

    builder.setSpan(clickable0, text.indexOf(word0), text.indexOf(word0) + word0.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.setSpan(clickable1, text.indexOf(word1), text.indexOf(word1) + word1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textview.setText(builder);
    textview.setMovementMethod(LinkMovementMethod.getInstance());

上面的代码是 2 个单词,你可以随意更改它们。我希望我没有错别字


推荐阅读