handle textview link click in my android app

前端 未结 12 928
迷失自我
迷失自我 2020-11-22 04:33

I\'m currently rendering HTML input in a TextView like so:

tv.setText(Html.fromHtml(\"test\"));

The HTML b

12条回答
  •  臣服心动
    2020-11-22 05:03

    Just to share an alternative solution using a library I created. With Textoo, this can be achieved like:

    TextView locNotFound = Textoo
        .config((TextView) findViewById(R.id.view_location_disabled))
        .addLinksHandler(new LinksHandler() {
            @Override
            public boolean onClick(View view, String url) {
                if ("internal://settings/location".equals(url)) {
                    Intent locSettings = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(locSettings);
                    return true;
                } else {
                    return false;
                }
            }
        })
        .apply();
    

    Or with dynamic HTML source:

    String htmlSource = "Links: Google";
    Spanned linksLoggingText = Textoo
        .config(htmlSource)
        .parseHtml()
        .addLinksHandler(new LinksHandler() {
            @Override
            public boolean onClick(View view, String url) {
                Log.i("MyActivity", "Linking to google...");
                return false; // event not handled.  Continue default processing i.e. link to google
            }
        })
        .apply();
    textView.setText(linksLoggingText);
    

提交回复
热议问题