2011-02-08 74 views
0

我有一個Android應用程序,運行加載某個頁面的WebView,也是應用程序的一部分。我想使用Android DatePicker從用戶獲取日期,並在WebView中的頁面內的輸入上設置值。在onPageFinished事件之外的Webview中注入Javascript(使用DatePicker在WebView的輸入上設置日期)

當用戶點擊輸入時打開DatePickerDialog,但是當我得到日期選擇的返回時,我無法運行loadUrl方法來設置輸入值,它給了我一個錯誤和應用程序崩潰。

的代碼(這是一個測試代碼,所以不介意這是一個有點斷章取義)一點點:

I類提供給的WebView:

public class webViewInterface { 
    public void openDatePicker(){ 
     showDialog(0); 
    } 
} 

在頁面上JavaScript函數,呼籲輸入點擊:

function openDatePicker() { 
     if (objAndroid != null) objAndroid.openDatePicker(); 
    } 

監聽的日期選擇

private DatePickerDialog.OnDateSetListener mDateSetListener = 
    new DatePickerDialog.OnDateSetListener() { 

     public void onDateSet(DatePicker view, int year, 
           int monthOfYear, int dayOfMonth) { 

      mWebView.loadUrl("javascript:setReturnDate();"); 
     } 
    }; 

JavaScript函數設置輸入

function setReturnDate() { 
     document.getElementById('txtAgency').value = '9999'; 
    } 

任何想法的值?

在此先感謝。

+0

使用`ADB logcat`,DDMS或DDM在Eclipse中查看LogCat並查看與錯誤相關的堆棧跟蹤。 – CommonsWare 2011-02-08 13:19:58

回答

2

感謝@CommonsWare的評論,我跟蹤誤差,並得到這個:

「android.view.ViewRoot $ CalledFromWrongThreadException:只有創建視圖層次可以觸摸其觀點原來的線程。」

Google搜索,並得到this forum thread

因此,爲了更新在運行時的界面視圖和外面的線程,你必須使用一個處理程序,接收郵件,並相應地執行動作。

所以我實現的處理程序的活動,即得到一個消息與一個int標識符和我想更新到網頁視圖中的數據:

final Handler handler = new Handler() { 
    public void handleMessage(Message msg) { 

     int cod = msg.getData().getInt("messageType"); 
     switch(cod){ 
      case 1: 
       String date = msg.getData().getString("datePickerValue"); 
       mWebView.loadUrl("javascript:setReturnDate('" + mId + "', '" + date + "');"); 
       break; 
      case 2: 
       showDialog(0); 
       break; 
     } 

    } 
}; 

我不得不改變的openDatePicker方法類使用處理程序,以及創建一個輸入參數,該參數是在頁面控制的id:

public void openDatePicker(String id){ 

     mId = id; //mId is a string declared in the Activity scope, so it can be used anywhere 
     Message msg = new Message(); 
     Bundle b = new Bundle(); 
     b.putInt("messageType", 2); 

     msg.setData(b); 
     handler.sendMessage(msg); 
    } 

讀和寫OTE的DatePicker的偵聽器日期選擇,以獲得數據併發送消息到處理程序:

private DatePickerDialog.OnDateSetListener mDateSetListener = 
    new DatePickerDialog.OnDateSetListener() { 

     public void onDateSet(DatePicker view, int year, 
           int monthOfYear, int dayOfMonth) { 

      Message msg = new Message(); 
      Bundle b = new Bundle(); 
      b.putInt("messageType", 1); 
      b.putString("datePickerValue", 
        String.format("%02d", dayOfMonth) + "/" + 
        String.format("%02d", (monthOfYear + 1)) + "/" + 
        String.valueOf(year)); 
      msg.setData(b); 
      handler.sendMessage(msg); 
     } 
    }; 

在網頁,js的功能得到了這樣:

function openDatePicker(id) { 
     if (objAndroid != null) objAndroid.openDatePicker(id); 
    } 

    function setReturnDate(id, date) { 
     var obj = document.getElementById(id); 
     if (obj != null) 
      obj.value = date; 
    } 

最後,輸入(文本框),得到了這樣:

TextBox obj = FindControl("txtDt") as TextBox; 

      if (obj != null) 
      { 
       obj.Attributes.Add("readonly", "readonly"); 
       obj.Attributes.Add("OnClick", "javascript:openDatePicker(this.id)"); 
      } 
相關問題