你能在 android webview 中自动链接日期吗

Can you autolink dates in android webview

我有这个html:

<p>
    <a href="http://en.wikipedia.org/wiki/Sauropelta">sawr-o-pel-te</a> meaning 'lizard shield'
</p>
<p>
    April 7th, 2015
</p>
<p>
    4/7/2015
</p>
<p>
    April 7th
</p>
<p>
    Next Monday 6th<br>
</p>
<p>
    202 E South St<br>
    Orlando, FL 32801
</p>
<h3>Quick Facts</h3>
<ul>
    <li>One of the most well-understood nodosaurids<span></span></li>
    <li>The earliest known genus of nodosaurid<span></span></li>
    <li>Measured about 5 meters (16.5 ft) long<span></span></li>
    <li>The tail made up nearly half of its body length</li>
</ul>
<span></span>

而且我想知道是否可以自动超链接日期以便当用户按下它们时您可以将它们添加到用户(phone 的)日历中? 这应该如何工作的一个很好的例子是 Gmail。当有日期或单词(明天、本周五等)时,它应该自动链接,以便将日期添加到日历中。

更新:

有谁知道有没有前任。 javascript 我可以添加到应用程序来为我完成这项工作吗?

您可以使用此 link 直接跳转到特定日期。

其中,日期 = YYYYMMDD 和模式 = day/month 根据您的需要。

希望这对您有所帮助。

是的,有可能。

使用WebViewClient方法shouldOverrideUrlLoading拦截任何包含特定值的link。假设您像这样创建 link:

<a href="#addtocalendar"> April 7th, 2015 - Add to Calendar</a>

现在,我们将使用 shouldOverrideUrlLoading 拦截点击并将事件添加到日历,即:

    mWebView.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView wView, String url)
        {
            if (url.contains("addtocalendar") ) {
               Calendar cal = Calendar.getInstance();              
               Intent intent = new Intent(Intent.ACTION_EDIT);
               intent.setType("vnd.android.cursor.item/event");
               intent.putExtra("allDay", false);
               intent.putExtra("rrule", "FREQ=YEARLY"); //optional recurring event
               intent.putExtra("beginTime", cal.getTimeInMillis());
               intent.putExtra("endTime", cal.getTimeInMillis()+3600000); // adds 1 hour
               intent.putExtra("title", "Event on April 7th, 2015");
               startActivity(intent);

               return true;
           }
      }
});

将以下权限添加到AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

shouldOverrideUrlLoading

boolean     shouldOverrideUrlLoading(WebView view, String url)

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView.

http://developer.android.com/reference/android/webkit/WebViewClient.html


Android 日历提供商

http://developer.android.com/guide/topics/providers/calendar-provider.html