我想在我的活动中展示某种“格式化”的文字。
此TextView中的某些单词(例如粗体格式)应该是可点击的。
当用户点击它们时,应显示一个Toast消息。
是否有可能在Android中执行此操作?
谢谢
答案 0 :(得分:5)
我发现对我来说可能是更好,更灵活的解决方案。
我可以在其中使用WebView,HTML和JavaScript函数来调用我的android应用程序中的方法。
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();
}
}
}