2012-03-14 18 views
1

TextViewで単語を強調表示する方法があるので、それをクリックして他のアクティビティを開始できますか?私は一種の辞書を作ってみたいと思っていますが、定義内のいくつかの単語は自己説明的なものではないので、私はそれらを強調表示してユーザーがクリックできると思っていました。TextViewでクリック可能なWord? Android

ありがとうございました。

+1

[この](http://stackoverflow.com/questionsをご覧ください/ 1697084/handle-textview-link-click-in-my-android-app) –

+0

...ここにポイントしているもの:http://blog.elsdoerfer.name/2009/10/29/clickable-urls-in- android-textviews/ – AJcodez

+0

この既存のSOの質問を確認してください:[クリック可能な単語TextView](http://stackoverflow.com/questions/5453102/clickable-words-in-a-textview) –

答えて

0

はあなたのデモを与える:

public class TextViewDemo extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      TextView txtInfo = new TextView(this); 
      SpannableString ss = new SpannableString("helloworldandroid:."); 
      ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, 
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
      ss.setSpan(new URLSpan("tel:4155551212"), 2, 5, 
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
      ss.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7, 
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
      ss.setSpan(new StrikethroughSpan(), 7, 10, 
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
      ss.setSpan(new UnderlineSpan(), 10, 16, 
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
      ss.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 15, 
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
      Drawable d = getResources().getDrawable(R.drawable.ic_launcher); 
      d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
      ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
      ss.setSpan(span, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
      txtInfo.setText(ss); 
      txtInfo.setMovementMethod(LinkMovementMethod.getInstance()); 
      setContentView(txtInfo); 
    } 

}

0

はこれで試してみてください。..

public class TextViewDemo extends Activity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 
     TextView textdata = (TextView) findViewById(R.id.textdata); 
      // To Display the Text as Link 
      textdata .setText(Html 
        .fromHtml("<a href='Write Ur Text Here'>Write Ur Text Here</a>")); 
      textdata .setMovementMethod(LinkMovementMethod.getInstance()); 

      // Performing action on TouchListener of ForgotPassword 
      textdata .setOnTouchListener(new View.OnTouchListener() { 

       public boolean onTouch(View v, MotionEvent event) { 
        // TODO Auto-generated method stub 
        Intent Forgotpwdintent = new Intent(); 
        Forgotpwdintent.setClass(TextViewDemo .this, 
          TargetActivity.class); 
        startActivity(Forgotpwdintent); 
        return true; 

       } 
      }); 
} 
} 
関連する問題