注入的Javascript到onPageFinished事件外的web视图(使用的DatePicker到一个web视图的输入设定一个日期) [英] Injecting Javascript into a Webview outside the onPageFinished Event (Using DatePicker to set a date on an input of a WebView)

查看:159
本文介绍了注入的Javascript到onPageFinished事件外的web视图(使用的DatePicker到一个web视图的输入设定一个日期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,运行的WebView加载某个页面,也有部分应用程序中。我想使用Android的DatePicker获得来自用户的日期和设定值上的web视图输入的页面中。

I have an Android app, running a WebView that loads a certain page, also part of the app. I want to use the Android DatePicker to get a date from the user and set the value on an input inside the page in the WebView.

在DatePickerDialog当用户点击输入​​打开,但是当我得到的日期选择的回归,我不能运行使用loadURL方法来设置值的输入,它给了我一个错误,应用程序崩溃。

The DatePickerDialog opens when the user clicks the input, but when i get the return of the date selection, i can't run the loadUrl method to set the value in the input, it gives me an error and the app crashes.

的code一点点(这是一个测试code,所以不介意这是一个有点断章取义):

A little bit of code (It's a test code, so don't mind if it is a bit out of context):

I级提供给web视图:

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';
    }

任何想法?

在此先感谢。

推荐答案

感谢@CommonsWare您的评论,我跟踪误差,并得到这样的:

Thanks @CommonsWare for the comment, i tracked the error and got this:

android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程

Google搜索,并获得<一href="http://stackoverflow.com/questions/1157814/how-to-access-original-activitys-views-from-spawned-background-service">this论坛主题

Googled it and got this forum thread

因此​​,为了在运行时,外它的线程来更新用户界面视图,你必须使用一个处理程序,接收邮件,并相应地执行动作。

So, in order to update a view in the UI at runtime and outside it's thread, you have to use a Handler, that receive messages and execute actions accordingly.

所以,我实现了一个处理程序的活动,也得到一个消息,我想更新到的WebView一个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);
    }

改写了的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)");
            }

这篇关于注入的Javascript到onPageFinished事件外的web视图(使用的DatePicker到一个web视图的输入设定一个日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆