1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

13  Android Epoch HTML/Javascript Calendar

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.04 MB, 688 trang )
























44 | Chapter 1: Getting Started



www.it-ebooks.info





Selected day:













>



File: CalendarView.java

Example 1-18.

import java.util.Date;

import

import

import

import

import

import

import

import

import

import

import

import

import

import



android.app.Activity;

android.content.Intent;

android.os.Bundle;

android.os.Handler;

android.util.Log;

android.view.View;

android.view.View.OnClickListener;

android.webkit.JsResult;

android.webkit.WebChromeClient;

android.webkit.WebSettings;

android.webkit.WebView;

android.widget.Button;

android.widget.ImageView;

android.widget.Toast;



import com.pfizer.android.R;

import com.pfizer.android.utils.DateUtils;

import com.pfizer.android.view.screens.journal.CreateEntryScreen;

public class CalendarViewActivity extends Activity

{

private static final String tag = "CalendarViewActivity";

private ImageView calendarToJournalButton;

private Button calendarDateButton;

private WebView webview;

private Date selectedCalDate;

private final Handler jsHandler = new Handler();

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

Log.d(tag, "Creating View ...");

super.onCreate(savedInstanceState);

// Set the View Layer

Log.d(tag, "Setting-up the View Layer");

setContentView(R.layout.calendar_view);



1.13 Android Epoch HTML/Javascript Calendar | 45



www.it-ebooks.info



// Go to CreateJournalEntry

calendarToJournalButton = (ImageView) this.findViewById(R.id.calendarToJournalButton);

calendarToJournalButton.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

Log.d(tag, "Re-directing -> CreateEntryScreen ...");

Intent intent = intent = new Intent(getApplicationContext(), CreateEntryScree

startActivity(intent);

}

});

// User-Selected Calendar Date

calendarDateButton = (Button) this.findViewById(R.id.calendarDateButton);

// Get access to the WebView holder

webview = (WebView) this.findViewById(R.id.webview);

// Get the settings

WebSettings settings = webview.getSettings();

// Enable Javascript

settings.setJavaScriptEnabled(true);

// Enable ZoomControls visibility

settings.setSupportZoom(true);

// Add Javascript Interface

webview.addJavascriptInterface(new MyJavaScriptInterface(), "android");

// Set the Chrome Client

webview.setWebChromeClient(new MyWebChromeClient());

// Load the URL of the HTML file

webview.loadUrl("file:///android_asset/calendarview.html");

}



public void setCalendarButton(Date selectedCalDate)

{

Log.d(tag, jsHandler.obtainMessage().toString());

calendarDateButton.setText(DateUtils.convertDateToSectionHeaderFormat(selectedCalDate.getTime

}

/**

*

* @param selectedCalDate

*/

public void setSelectedCalDate(Date selectedCalDate)

{

this.selectedCalDate = selectedCalDate;

}



46 | Chapter 1: Getting Started



www.it-ebooks.info



/**

*

* @return

*/

public Date getSelectedCalDate()

{

return selectedCalDate;

}

/**

* JAVA->JAVASCRIPT INTERFACE

*

* @author wagied

*

*/

final class MyJavaScriptInterface

{

private Date jsSelectedDate;

MyJavaScriptInterface()

{

// EMPTY;

}

public void onDayClick()

{

jsHandler.post(new Runnable()

{

public void run()

{

// Java telling Javascript to do things

webview.loadUrl("javascript: popup();");

}

});

}

/**

* NOTE: THIS FUNCTION IS BEING SET IN JAVASCRIPT User-selected Date in

* WebView

*

* @param dateStr

*/

public void setSelectedDate(String dateStr)

{

Toast.makeText(getApplicationContext(), dateStr, Toast.LENGTH_SHORT).show();

Log.d(tag, "User Selected Date: Javascript -> Java : " + dateStr);

// Set the User Selected Calendar date

setJsSelectedDate(new Date(Date.parse(dateStr)));

Log.d(tag, "java.util.Date Object: " + Date.parse(dateStr).toString());



}

private void setJsSelectedDate(Date userSelectedDate)

{

jsSelectedDate = userSelectedDate;

}

public Date getJsSelectedDate()



1.13 Android Epoch HTML/Javascript Calendar | 47



www.it-ebooks.info



{

}



return jsSelectedDate;

}

/**

* Alert pop-up for debugging purposes

*

* @author wdavid01

*

*/

final class MyWebChromeClient extends WebChromeClient

{

@Override

public boolean onJsAlert(WebView view, String url, String message, JsResult result)

{

Log.d(tag, message);

result.confirm();

return true;

}

}



}



@Override

public void onDestroy()

{

Log.d(tag, "Destroying View!");

super.onDestroy();

}



For debugging purposes, a MyWebChromeClient is created and the onJsAlert() method is overriden.



1.14 Sharing Java classes from another Eclipse Project

Ian Darwin



Problem

You want to use a class from another project, but don't want to copy-and-paste



Solution

Add the project as a "referenced project", and Eclipse (and DEX) will do the work.



Discussion

You often need to re-use classes from another project. In JPSTrack GPS tracking program, the Android version borrows classes like the file I/O module from the Java SE

version. You surely do not want to copy and paste classes willy-nilly from one project

into another, because this makes maintenance improbable.



48 | Chapter 1: Getting Started



www.it-ebooks.info



Figure 1-28.



All you really have to do is declare the project containing the needed classes (the Java

SE version in this case) as a referenced project on the build path. Select Project->Properties->Build Path, select Project, and click "Add". In the screenshot I am adding the

SE project "jpstrack" as a dependency on the Android version project "jpstrack.android".

Since you are probably keeping both projects under source control (if these are programs you ever ship, you should!), remember to tag both projects when you release the

Android project - one of the points in favor of source control is that you need to be able

to re-create exactly what you shipped.



1.15 Referencing libraries to implement external functionality

Rachee Singh



Problem

You need to reference an external library in your source code



1.15 Referencing libraries to implement external functionality | 49



www.it-ebooks.info



Xem Thêm
Tải bản đầy đủ (.pdf) (688 trang)

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×