在一个TextView的可点击的话 [英] Clickable words in a TextView

查看:113
本文介绍了在一个TextView的可点击的话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示某种在我的活动格式化的文本。

I want to show some sort of "formatted" text in my activity.

这TextView的中的一些话(例如粗体格式化)应该点击。

Some words within this TextView (for example bold formatted) should be clickable.

当应显示在用户点击他们敬酒消息。

When the user clicks on them a toast message should be displayed.

有没有可能做到这一点在Android中?

Are there possibility to do that in Android?

感谢您

推荐答案

我觉得可能是更好的,更灵活的解​​决方案适合我。

I found probably the better and more flexible solution for me.

我可以使用的WebView,HTML和JavaScript的功能在它该叫我的Andr​​oid应用程序的方法。

I can use WebView, HTML and JavaScript-Functions within it which call methods in my android application.

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();
        }
    }
}

这篇关于在一个TextView的可点击的话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆