开发者

Clickable words in a TextView

开发者 https://www.devze.com 2023-02-20 08:33 出处:网络
I want to show some sort of \"formatted\" text in my activity. Some words within this TextView (for example bold formatted) should be clickable.

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

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

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

Are there possibili开发者_JS百科ty to do that in Android?

Thank you


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

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

精彩评论

暂无评论...
验证码 换一张
取 消