2011-03-27 43 views
2

我想在我的活動中顯示某種「已格式化」的文本。TextView中的可點擊單詞

此TextView中的某些單詞(如粗體格式)應該是可點擊的。

當用戶點擊它們時,應顯示吐司消息。

有沒有可能在Android中做到這一點?

謝謝

+0

請檢查這個answer yjw 2011-03-28 02:47:26

回答

5

我發現可能是更好的,更靈活的解決方案適合我。

我可以在我的android應用程序中使用WebView,HTML和JavaScript函數調用方法。

public class MainScreen extends Activity 
{ 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     WebView webview = new WebView(this); 
     setContentView(webview); 
     String data = "Test <u onClick=\"showAndroidToast('test')\">it</u>"; 
     String js_data = "<script type=\"text/javascript\">function showAndroidToast(toast){Android.showToast(toast);}</script>"; 
     webview.loadData(data + js_data, "text/html", "utf-8"); 

     WebSettings webSettings = webview.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 

     webview.addJavascriptInterface(new JavaScriptInterface(this), "Android");   
    }  

    public class JavaScriptInterface 
    { 
     Context mContext; 

     /** Instantiate the interface and set the context */ 
     JavaScriptInterface(Context c) 
     { 
      mContext = c; 
     } 

     /** Show a toast from the web page */ 
     public void showToast(String toast) 
     { 
      Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); 
     } 
    } 
}