在一个TextView的可点击的话的话、TextView

由网友(余生不过一个你)分享简介:我要显示某种在我的活动格式化的文本。I want to show some sort of "formatted" text in my activity.这TextView的中的一些话(例如粗体格式化)应该点击。Some words within this TextView (for example bold...

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

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();
        }
    }
}
阅读全文

相关推荐

最新文章