2012-03-14 98 views
1

有沒有辦法在TextView中突出顯示一個單詞,這樣您就可以點擊它並啓動其他Activity?我想製作一種字典應用程序和定義中的某些字詞不是自我解釋的,所以我想當我突出顯示它們並且用戶可以點擊它們時,其他活動應該以其他定義開始。在TextView中可點擊的單詞? Android

謝謝。

+1

請看看[這裏](http://stackoverflow.com/questions/1697084/handle-textview-link-click-in-my-android-app) – 2012-03-14 01:32:19

+0

...哪指向這裏:http://blog.elsdoerfer.name/2009/10/29/clickable-urls-in- android-textviews/ – AJcodez 2012-03-14 01:40:06

+0

檢查這個存在的SO問題:[Clickable words在TextView中](http://stackoverflow.com/questions/5453102/clickable-words-in-a-textview) – 2012-03-14 05:12:50

回答

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; 

       } 
      }); 
} 
} 
相關問題