android 生词本 句子取词

不废话直接上代码:

public class WordActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word);
        textView = (TextView) findViewById(R.id.text);
        textView.setText("we are famle is good nice wrongwe are famle is good nice wrongwe are famle is good nice wrongwe are famle is good nice wrongwe are famle is good nice wrongwe are famle is good nice wrongwe are famle is good nice wrongwe are famle is good nice wrongwe are famle is good nice wrongwe are famle is good nice wrong", TextView.BufferType.SPANNABLE);//点击每个单词响应
        getEachWord(textView);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
    /**
     * 点击响应方法getEachWord()内容如下
     */
    public void getEachWord(TextView textView){
        Spannable spans = (Spannable)textView.getText();
        Integer[] indices = getIndices(textView.getText().toString().trim() + " ", ' ');
        int start = 0;
        int end = 0;
        // to cater last/only word loop will run equal to the length of indices.length
        for (int i = 0; i < indices.length; i++) {
            ClickableSpan clickSpan = getClickableSpan();
//          to cater last/only word
            end = (i < indices.length ? indices[i] : spans.length());
            spans.setSpan(clickSpan, start, end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            start = end + 1;
        }
//         改变选中文本的高亮颜色
//        textView.setHighlightColor(Color.BLUE);
    }
    private ClickableSpan getClickableSpan(){
        return new ClickableSpan() {
           @Override
           public void onClick(View widget) {
               TextView tv = (TextView) widget;
               Spannable spans = (Spannable)textView.getText();
               String s = tv.getText().subSequence(tv.getSelectionStart(),tv.getSelectionEnd()).toString();
               Log.e("tapped on:", s);
           }
            @Override
            public void updateDrawState(TextPaint ds) {
               ds.setColor(Color.BLACK);
               ds.setUnderlineText(false);
            }
        };
    }
    public Integer[] getIndices(String ss,char c){
        int pos = ss.indexOf(c, 0);
        List<Integer> integers = new ArrayList<>();
           while (pos != -1) {
               integers.add(pos);
               pos = ss.indexOf(c, pos + 1);
           }
       return integers.toArray(new Integer[0]);
    }








}

具体的实现过程是:1、将TextView内容转换为Spannable对象;2、使用getIndices方法将文本内容根据空格划分成各个单词;3、为每个单词添加ClickableSpan;4、在ClickableSpan对象实例化过程中复写onClick方法,取出被点击的部分的文本内容。

这样的实现过程有几个注意点:

1、ClickableSpan有默认的属性,可点击的超链接默认有下划线,并且是蓝色的,如果需要改变默认属性可以在复写的updateDrawState()方法中加入setColor()和setUnderlineText()方法。

2、上面提到的setColor()设定的是span超链接的文本颜色,而不是点击后的颜色,点击后的背景颜色(HighLightColor)属于TextView的属性,Android4.0以上默认是淡绿色,低版本的是黄色。改颜色可以使用textView.setHighlightColor(Color.BLUE)来实现。

3、例子中将点击获取的文本使用Log打印出来,可以发现这里的文本切割方法仅仅针对空格,所以取得的单词会有标点符号等,如果要获取严格的单词,则还需对获取的文本进一步处理。


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值