Android:如何从TextView链接本地HTML文件



我知道这里有很多关于阅读本地HTML文件的问题,但我还没有找到任何能满足我需求的问题。

我想弹出一个DialogFragment,显示一条带有链接的消息,单击该链接并读取本地HTML文件(最好是从默认的web浏览器中读取)。

到目前为止我所拥有的:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
        alertDialogBuilder.setMessage(Html.fromHtml("I have read and agree to the " + "<a href="file:///android_asset/privacy.html">Terms and Conditions</a>" + " and the " + "<a href="http://www.samsung.com/global/business/mobile/info/privacy.html">Privacy Policy</a>"));
        alertDialogBuilder.setCancelable(false);
        //rest of code...
    }

指向在线版本的第二个链接就像一个魅力。第一个链接应该指向资产文件夹中的本地privacy.html文件。点击第一个链接时,出现错误:

    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///android_asset/privacy.html (has extras) }

有人对这种情况有什么建议吗?我尝试了多个不同的HTML文件,结果都是一样的。我是否必须使用我自己的WebView来进行类似的操作?似乎应该有一种方法让Android在点击链接时从默认浏览器查看本地HTML文件。。。

从技术上讲,它不起作用,因为Link是html标记。它将在webview中工作。尽管如此,如果你想用textview来实现这一点,你已经自定义了你的代码试试这个代码:

public class CustomLinkMovementMethod extends LinkMovementMethod
{
private static Context movementContext;
private static CustomLinkMovementMethod linkMovementMethod = new CustomLinkMovementMethod();
public boolean onTouchEvent(android.widget.TextView widget, android.text.Spannable buffer, android.view.MotionEvent event)
{
    int action = event.getAction();
    if (action == MotionEvent.ACTION_UP)
    {
        int x = (int) event.getX();
        int y = (int) event.getY();
        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();
        x += widget.getScrollX();
        y += widget.getScrollY();
        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);
        URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
        if (link.length != 0)
        {
            String url = link[0].getURL();
            if (url.startsWith("https"))
            {
                Log.d("Link", url);
                Toast.makeText(movementContext, "Link was clicked", Toast.LENGTH_LONG).show();
            } else if (url.startsWith("tel"))
            {
                Log.d("Link", url);
                Toast.makeText(movementContext, "Tel was clicked", Toast.LENGTH_LONG).show();
            } else if (url.startsWith("mailto"))
            {
                Log.d("Link", url);
                Toast.makeText(movementContext, "Mail link was clicked", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    }
    return super.onTouchEvent(widget, buffer, event);
}
public static android.text.method.MovementMethod getInstance(Context c)
{
    movementContext = c;
    return linkMovementMethod;
}

这应该以以下方式从文本视图调用:

textViewObject.setMovementMethod(CustomLinkMovementMethod.getInstance(context));
myTextView.setText(Html.fromHtml(readTxt())); 

//此函数将返回可以在文本视图中设置的字符串。字符串有html代码,所以使用html.fromHtml

 private String readTxt(){
 InputStream inputStream = getResources().openRawResource(R.raw.My_html_file);
 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 int i;
 try {
 i = inputStream.read();
  while (i != -1)
  {
   byteArrayOutputStream.write(i);
   i = inputStream.read();
  }
  inputStream.close();
  } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
  return byteArrayOutputStream.toString();
}
 }

这个已经回答

相关内容

  • 没有找到相关文章

最新更新