ï¼Tips ãTextView ã使ãããªãããã 表示編 ãã ãã®ï¼
android.text.Html#fromHtml() 㧠<A> ã¿ã°ã¨ <IMG> ã¿ã°ã使ã
ãåå ãåã å ã«å¼ãç¶ã㦠TextView ã®ï¼åç®ã§ãã
ãååã¾ã§ã®æ¹æ³ã§å¤§å¡ã®ããã¹ã表ç¾ãå¯è½ã«ãªãã¾ãããããããä½ãããã®èª¬ææãç´¹ä»æãè¨è¿°ããå ´åã«ããã説æãããµã¤ãã«ã¯ãªãã¯ã²ã¨ã¤ã§ãªã³ã¯åºæ¥ãã°ãªã«ãã¨ä¾¿å©ã§ããã¾ããç»åãæä¸ã«å·®ãæãäºãåºæ¥ãã°è¤éãªæ°å¼ãªã©ã LaTeX ãªã©ã§ä½æããç»åãå©ç¨ãããªã©ãã¦è¡¨ç¤ºãå¯è½ã«ãªãã¾ãã
<A> ã¿ã°ã® HREF å±æ§ã§æå®ãã URL ããã©ã¦ã¶ã§éã
ãåã« fromHtml() ã« <A> ã¿ã°ãå«ããããã¹ããå¼æ°ã¨ãã¦æ¸¡ãã¦ããè¦æ ãã¯å¤ãã£ã¦ããªã³ã¯ãæ¼ãã¦ãä½ã®å¤åãããã¾ãããã©ããã¦ãªã®ã§ããããï¼çç±ã¯ï¼ã¤ããã¾ãã
ï¼. AndroidManifest.xml ã¸ã®è¨å®ã§ã¤ã³ã¿ã¼ãããã¸ã®ã¢ã¯ã»ã¹æ¨©ãå¾ã
ã¾ãï¼ã¤ç®ã®çç±ã¯ãããã©ã«ãã§ã¯ã¢ããªã±ã¼ã·ã§ã³ãã¤ã³ã¿ã¼ãããã¸ã®ã¢ã¯ã»ã¹ã許å¯ãã¦ããªãããã§ããããããäºåã«è¨å®ãããã¨ã§éä¿¡ã¯å¯è½ã«ãªãã¾ããè¨å®ã¯ AndroidManifest.xml ã§è¡ãªãã¾ãã
ãã®ä¸æã AndroidManifest.xml ã«æ¸ãå ãããã¨ã§ã¢ããªã±ã¼ã·ã§ã³ã¯ã¤ã³ã¿ã¼ãããã¨ã®éä¿¡ãå¯è½ã«ãªãã¾ãã
ï¼. TextView#setMovementMethod() ã« LinkMovementMethod ã渡ã
ï¼ã¤ç®ã®çç±ã¯ãHtml#fromHtml() ã®å¦çéç¨ã§ <A> ã¿ã°ãè¦ã¤ãã㨠URLSpan ãçµã¿è¾¼ã¾ãã¾ãããã®ã¯ã©ã¹ã«ã¯ URLSpan#onClick(View widget) ã¨ããã¡ã½ãããããããã®ã¡ã½ãããå¼ã³åºããããã¨ã§å¤é¨ã®WEBãã©ã¦ã¶ãèµ·åã HREF å±æ§ã§è¨å®ãã URL æ å ±ããã©ã¦ã¶ã«å¼ã渡ããããã® URL ãå ã«ãã©ã¦ã¶ãWEBãã¼ã¸ã表示ããããããã°ã©ã ããã¦ãã¾ãããããããã®ã¾ã¾ã®ç¶æ ã§ã¯ URLSpan#onClick() ãå¼ã³åºããå¼ã³åºãå½¹ããåå¨ãã¦ããªãã®ã§ããã TextView ã«ãããªã³ã¯ãã¯ãªãã¯ããã㨠URLSpan ã«ä¼ããå½¹åããç¨æããå¿ è¦ãããã¾ããä»å㯠LinkMovementMethod ã使ãã¾ãããã®ã¤ã³ã¹ã¿ã³ã¹ã TextView#setMovementMethod() ã®å¼æ°ã¨ãã¦æ¸¡ãã° onClick() ã®ãå¼ã³åºãå½¹ãã«ãªã£ã¦ããã¾ãã
ãµã³ãã«ã³ã¼ãã§ãã
public class TextViewTest extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); // LinkMovementMethod ã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ãã¾ã MovementMethod movementmethod = LinkMovementMethod.getInstance(); // TextView ã« LinkMovementMethod ãç»é²ãã¾ã textview.setMovementMethod(movementmethod); // <a>ã¿ã°ãå«ããããã¹ããç¨æãã¾ã String html = "ãªã³ã¯ãã¯ãªãã¯ãã㨠<a href=\"http://www.google.co.jp/\">Google</a> ã表示ããã¾ã"; // URLSpan ãããã¹ãã«ãçµã¿è¾¼ã¿ã¾ã CharSequence spanned = Html.fromHtml(html); textview.setText(spanned); setContentView(textview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); } }
æåã« static ã¡ã½ããã§ãã getInstance() ãå¼ã³åºã LinkMovementMethod èªèº«ã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ãã¾ãã 次㫠URLSpan ã® onClick() ã®ãå¼ã³åºãå½¹ãã¨ã㦠TextView ã«åå¾ããã¤ã³ã¹ã¿ã³ã¹ãç»é²ãã¾ããå¾ã¯ãååã¨åæ§ã§ããç¹å¥ãªãã¨ã¯ããã¾ããã
ã§ã¯ãå®éã«åããã¦ã¿ã¾ãã
ãªã³ã¯ãå®éã«ã¯ãªãã¯ãã¦ã¿ã¾ãã
ãã©ã¦ã¶ãèµ·åããç¡äº Google ã表示ããã¾ããã
以ä¸ã§ <A> ã¿ã°ã使ããããã«ãªãã¾ããã
ããã§çæç¹ãï¼ã¤ãå®ã¯ãã®ãããªæéããããã¨ãããã¹ãä¸ã«ãªã³ã¯ãä»ããæ¹æ³ãããã¾ãã
TextView#setAutoLinkMask() ã¨ããã¡ã½ããã« Linkify.WEB_URLS ã¨ããå®æ°ãå¼æ°ã¨ãã¦æ¸¡ãããæ¹ã§ãããã®ããæ¹ã§ã㨠Html#fromHtml() ã¡ã½ããã使ãå¿
è¦ãããã¾ãããæä¸ã«ã http://ã ããè¦ã¤ããã¨èªåçã«ãªã³ã¯ãå¼µã£ã¦ããã¾ãããããããã®è¨å®ã¨å
ã»ã©ã¾ã§èª¬æãã¦ããè¨å®ã¯å
±åã§ããªãããã§ã<A> ã¿ã°ã§è¨å®ããæ¹ã®ãªã³ã¯ãæ¶ãã¦ãã¾ãå®è³ªç¡å¹ã«ãªã£ã¦ãã¾ãã¾ããç¶æ³ã«å¿ãã¦ä½¿ãåããå¿
è¦ãããããã§ãã
<IMG> ã¿ã°ãç¨ãã¦ããã¹ãä¸ã«ç»åãæ¿å ¥ãã
ãåç´ã« <IMG> ã¿ã°ãç¨ãã¦ãæå³ä¸æãªç»åã表示ãããã ãã§ç®çã®ç»åã¯è¡¨ç¤ºããã¾ããããããã <IMG> ã¿ã°ã® SRC å±æ§ã«ã©ããªãã¹ãæ¸ãã°ããããä¸æã§ãã
å®ã®ã¨ãããæ£è§£ã¯ãä½ãæ¸ãã¦ããã¡ãã§ãã
SRC ã«ä½ã¨æ¸ãã¦ãããã¨ç¡è¦ãã¦ãæå³ä¸æãªç»åãï¼å®éã¯ãªã½ã¼ã¹ã® R.drawable.unknown_image ï¼ã TextView ã«è²¼ãä»ããããã«ãªã£ã¦ãã¾ãããã㯠Html#fromHtml() ã®ããï¼ã¤ã®å¼æ°éãã®ã¡ã½ããã»ã»ã»
public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
ã大ãã«é¢ä¿ãã¦ãã¾ãã
ä»ã¾ã§ä½¿ç¨ãã¦ãã Html#fromHtml(String source) ã使ãã¨å é¨çã«ä¸è¨ã®ã¡ã½ãããå¼ã°ãã第ï¼å¼æ° source ã第ï¼å¼æ° null ã第ï¼å¼æ° null ãå¼ã渡ããã¾ãã第ï¼å¼æ°ã« null ã渡ãããã¨ä¾ã®ãæå³ä¸æãªç»åãã表示ãããä»çµã¿ã«ãªã£ã¦ãã¾ãããããåé¿ããçºã«ã¯å®ä½ã®ãã Html.ImageGetter ã¤ã³ã¹ã¿ã³ã¹ãæ示çã«æ¸¡ãã¦ãããªããã°ããã¾ããã
Html.ImageGetter ã¨ã¯ã»ã»ã»
android.text.Html.ImageGetterHtml.ImageGetter ã¯ãã£ãï¼ã¤ã®æ½è±¡ã¡ã½ãããæã¤ã¤ã³ã¿ã¼ãã§ã¤ã¹ã§ãã
public abstract Drawable getDrawable (String source)
å¼æ°ã« SRC å±æ§ã§è¨å®ããæåå source ãå¼ã渡ãããã¨ãæ»ãå¤ã«æç»ãªãã¸ã§ã¯ãã® Drawable ãè¿ã£ã¦ããããã«å®è£ ããªããã°ãªãã¾ãããããã¦ããã®æ½è±¡ã¡ã½ãããå®è£ ããã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãä½ãã fromHtml() ã®ç¬¬ï¼å¼æ°ã«æ¸¡ãã° TextView ã«ç»åã表示ãããã¨ããä»çµã¿ã§ãã
ãµã³ãã«ã³ã¼ãã§ãã
public class TextViewTest extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); // <img> ã¿ã°ä»ãã®æååãä½ãã¾ã String html = "æä¸ã«ç»å<img src=\"icon\">ãæ¿å ¥ããã¾ã"; // 第ï¼å¼æ°ã® ImageGetter ã¯å¿åã¤ã³ãã¼ã¯ã©ã¹ã¨ãã¦å®ç¾©ã第ï¼å¼æ°ã¯ null CharSequence text = Html.fromHtml(html, new Html.ImageGetter(){ public Drawable getDrawable(String source){ // ç»åã®ãªã½ã¼ã¹IDãåå¾ int id = getResources().getIdentifier(source, "drawable", getPackageName()); // ãªã½ã¼ã¹IDãã Drawable ã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ Drawable d = getResources().getDrawable(id); // åå¾ããå ç»åã®ãµã¤ãºãåå¾ãã表示ç»åã®ãµã¤ãºã¨ãã int w = d.getIntrinsicWidth(); int h = d.getIntrinsicHeight() d.setBounds(0, 0, w, h); return d; } }, null); textview.setText(text); setContentView(textview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); } }
æåã« <IMG> ã¿ã°ãå«ã¾ããHTMLã¹ã¿ã¤ã«ã®ããã¹ãç¨æãã¦ããã¾ãã次㫠Html#fromHtml() ãå¼ã³åºãã¾ãã第ï¼å¼æ°ã«ã¯ä»ã¾ã§ã©ããHTMLã¹ã¿ã¤ã«ã®ããã¹ããã第ï¼å¼æ°ã«ã¯ Html.ImageGetter ã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£ ããã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã渡ãã¾ãããä»åã¯ä¾¿å®ä¸ãå¿åã¤ã³ãã¼ã¯ã©ã¹ã¨ãã¾ããããã¡ãã¨å¥ã®ã¯ã©ã¹ã¨ãã¦å®ç¾©ããã»ããé常ã¯è¯ãããããã¾ããã次ã«å¿åã¤ã³ãã¼ã¯ã©ã¹ã® getDrawable() ã¡ã½ããã®å ·ä½çãªå®è£ ã§ãããä»åãã¾ãç¸ãå¤ãããç»åæºåã®æéãæããã§ãªã½ã¼ã¹ã«ãã R.drawable.icon ãå©ç¨ãããã¨ã«ãã¾ããå®éã®ã±ã¼ã¹ã§ã¯ãã¼ã«ã«ãã¼ã¿ãªã©ãç¨ããã§ããããããããã§ã¯ã Drawable ã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ãã¦ããã ãç¨åº¦ã«ç解ãèªã¿é£ã°ãã¦ããã¦ãã ããã次ã«ã表示ç»åã®ãµã¤ãºã決ããªããã°ãªãã¾ãããä½ãããªãã¨ç¸¦ï¼æ¨ªï¼ã®ãµã¤ãºã§è¡¨ç¤ºããã¾ãï¼ããªãã¡è¦ãã¾ããï¼ããµã¤ãºã¯ã±ã¼ã¹ã»ãã¤ã»ã±ã¼ã¹ã§ãã®ã§å¿ è¦ã«ãããã¦èª¿æ´ãã¦ãã ãããããã¦æå¾ã« Drawable ã¤ã³ã¹ã¿ã³ã¹ãè¿ãã¦ãä»èãã§ãã
ã§ã¯ãå®éã«åããã¦ã¿ã¾ãã
ããã¹ãä¸ã«ç»åãæ¿å ¥ããã¾ããã
以ä¸ã§ <IMG> ã¿ã°ãç¨ãã¦ããã¹ãä¸ã«ç»åãæ¿å ¥ããæ¹æ³ã®èª¬æã¯çµäºã§ãã
次åã¯ã»ã»ã»
æªå®ã§ãã
TextView ã®è¡¨ç¤ºã®ç¶ãã¨ã㦠Html#fromHtml() ã使ããã« Span ãç¨ããæ¹æ³ãæ¸ããããªããã¨ãèãã¦ã¾ããå¥ã®è©±ã«ãªãããããã¾ãããé©å½ã«ãã¥ã¼ã¹ãã¿ã«ãªãå¯è½æ§ãä¸çªé«ãããã»ã»ã»ã