在android中输入单词可点击

时间:2016-08-03 14:22:48

标签: android

我尝试了不同的方法使editText完全按照textView的工作方式工作,但是当我开始输入str的内容时,app停止工作。这是我试过的不同方法的代码,有人可以告诉我我没有得到什么

enter code here
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.span_things);
    String str = " This is as a result of instances in this matter and "
            + "as it is has look as this. So and this3 goes on manthis "
            +"and also thisman like this gen 2:6,";
    String regex = "\\bthis\\b";
    textView = (TextView)findViewById(R.id.span_test);
    SpannableString spstr = new SpannableString(str);
    Pattern patern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    Matcher matcher = patern.matcher(spstr.toString());
    while(matcher.find()){
        spstr.setSpan(new ClickableSpan(){
            @Override
            public void onClick(View wiget){
                Toast.makeText(SpanThingsActivity.this,
                        "Clicked" + "styledString. ",                       Toast.LENGTH_SHORT).show();
            };}, matcher.start(), matcher.end(), 0);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
    textView.setText(spstr);

    editText = (EditText)findViewById(R.id.spanthings);
    editText.addTextChangedListener(textWatcher);
}
private final TextWatcher textWatcher = new TextWatcher(){

    @Override
    public void afterTextChanged(Editable s) {
        try{
            String strW = s.toString();
            String regex = "\\bthis\\b";
            SpannableString spsr = new SpannableString(strW);
            Pattern path = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher match = path.matcher(spsr.toString());
            while(match.find()){
                spsr.setSpan(new ClickableSpan(){
                    @Override
                    public void onClick(View wiget){
                        Toast.makeText(SpanThingsActivity.this,
                                "Clicked" + "styledString. ", Toast.LENGTH_SHORT).show();
                    };}, match.start(), match.end(), 0);
                //((EditText)s).setMovementMethod(LinkMovementMethod.getInstance());
                editText.setMovementMethod(LinkMovementMethod.getInstance());
        //      if(s.length() > 0){
        //          s.replace(0, spsr.length(), spsr);}
            }
            //((EditText) s).setText(spsr);
            //editText.setText(spsr);
            s.replace(0, s.length(), spsr.toString());
//      if(s.length() > 0){
//          s.replace(0, spsr.length(), spsr);}
        ((TextView) s). setMovementMethod(LinkMovementMethod.getInstance());
        }catch(Exception ie){}      
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub

    }

};

0 个答案:

没有答案