2017-01-05 5 views
0

SpannableStringの特定の部分文字列を編集可能にしようとしています。問題があるSpannableStringの特定の部分文字列を編集可能にする

myTxtView.setFocusable(true); 
myTxtView.setEnabled(true); 
myTxtView.setClickable(true); 
myTxtView.setFocusableInTouchMode(true); 
myTxtView.setCursorVisible(true); 
myTxtView.setInputType(InputType.TYPE_CLASS_TEXT); 
myTxtView.requestFocus(); 

enter image description here

を使用して

ストリングを編集可能にすることもイムmatcher.find()を使用して、私の基準に一致するすべての部分文字列をクリッカブルを識別して作ることができるイムとClickableSpan()

一致した部分文字列だけでなく、テキストビュー全体が編集可能になります。

は、ここで私は成功しませんEditable.Factory()と少し周りプレイしたコード

final SpannableString hashText = new SpannableString(myTxtView.getText().toString()); 

while (matcher2.find()) { 

    //find and match strings 

    hashText.setSpan(new ClickableSpan() { 

    @Override 
    public void onClick(View widget) { 

    //do something on click  

    } 

    @Override 
    public void updateDrawState(TextPaint ds) { 
     super.updateDrawState(ds); 
     ds.setColor(Color.parseColor("#A8000000")); 
     ds.setUnderlineText(true); 

    } 

    },matcher2.start(),matcher2.end(),1); 

} 
    myTxtView.setText(hashText); 
    myTxtVIew.setMovementMethod(LinkMovementMethod.getInstance()); 
    myTxtView.setFocusable(true); 
    myTxtView.setEnabled(true); 
    myTxtView.setClickable(true); 
    myTxtView.setFocusableInTouchMode(true); 
    myTxtView.setCursorVisible(true); 
    myTxtView.setInputType(InputType.TYPE_CLASS_TEXT); 
    myTxtView.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); 

です。どのようにしてそれが私の場合に当てはまるかわからない。あなたが欲しいものを実行するためにClickableSpanを使用する必要が

答えて

1

は、ここでいくつかのcodeSnippetはあなた

SpannableString spanString = new SpannableString(text); 
    Matcher matcher = Pattern.compile("@([A-Za-z0-9_-]+)").matcher(spanString); 

    while (matcher.find()) 
    { 
     spanString.setSpan(new ForegroundColorSpan(Color.parseColor("#0000FF")), matcher.start(), matcher.end(), 0); //to highlight word havgin '@' 
     final String tag = matcher.group(0); 
     ClickableSpan clickableSpan = new ClickableSpan() { 
      @Override 
      public void onClick(View textView) { 
       Log.e("click", "click " + tag); 
       String searchText=tag.replace("@",""); //replace '@' with blank character to search on google. 
       Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.co.in/search?q=" + searchText)); 
       startActivity(browserIntent); 
      } 
      @Override 
      public void updateDrawState(TextPaint ds) { 
       super.updateDrawState(ds); 

      } 
     }; 
     spanString.setSpan(clickableSpan, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    } 

は返信用androidgig.com

Output

+0

おかげから参照を取るのに役立ちますこれは。明確にするために、私のコードは、色付けされていて色付けされた文字列をクリック可能にするだけで動作し、クリックに反応します。私の問題は、クリック可能な/色付きのテキストのみを編集可能にする方法を見つけ出すことができないということです。編集可能とは、キーボード入力によるテキストの編集を意味します。 – Mcorv

関連する問題