我想在我的 Activity 中显示某种“格式化”文本。
此 TextView 中的某些单词(例如粗体格式)应该是可点击的。
当用户单击它们时,应显示一条 toast 消息。
在 Android 中是否有可能做到这一点?
谢谢
最佳答案
我找到了可能对我来说更好、更灵活的解决方案。
我可以在其中使用 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();
}
}
}
关于android - TextView 中的可点击单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5453102/