android - TextView 中的可点击单词

标签 android textview

我想在我的 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/

相关文章:

android - Horizo​​ntalScrollView与Recyclerview滑动android冲突

android - TextView 大小不会在 setTextSize 上正确调整大小

android - 删除堆叠的 TextView 之间的空间

android - 将 TableLayout 的一行中的 TextView 和 EditText 设置为相同的高度?

android - 将日期格式化为mysql格式

android - 为 textView Android 设置文本颜色

android - 将默认文本设置为 android 中的微调器 View

android - LayerList 或 LevelList XML 项中非位图可绘制对象的内联定义

android - 如何在 Android 中将 JSON ARRAY 数据发布到服务器

android - 无法通过servlet向mysql插入数据