webview日期选择器在Android模拟器中工作,但不在设备上?

时间:2015-03-01 11:27:14

标签: javascript android jquery datepicker android-webview

我已经使用了这里发布的代码示例calling date picker from javascript,这在android模拟器中工作正常。当我在Web视图中单击日期输入框时,会出现android日期选择器,我可以选择它。

然而,当我在我的设备上测试它时,我只是得到一个标准的文本输入字段,就好像没有调用该方法一样。有没有人有什么建议?我正在使用的完整代码是:

public class MainActivity extends ActionBarActivity {

  public  WebView mWebView;

    @Override
    protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    final MyJavaScriptInterface myJavaScriptInterface = new MyJavaScriptInterface(this);
    mWebView.addJavascriptInterface(myJavaScriptInterface, "MyJavaScriptInterface");

    mWebView.loadUrl("http:// mywebsite.html");
}

    // Classe de prise en charge du java privé
    public class MyJavaScriptInterface {
        public String m_szTagId;

        Context mContext;

        MyJavaScriptInterface(Context c) {
            mContext = c;
        }

        public void openDatePickerDialog(String szTagId) {
            m_szTagId = szTagId;
            Calendar c = Calendar.getInstance();
            DatePickerDialog dp = new DatePickerDialog(mContext, new OnDateSetListener() {
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                    String szDate = String.format("%04d/%02d/%02d", year, monthOfYear + 1, dayOfMonth);
                    mWebView.loadUrl("javascript:callFromActivity_RetDate(\"" + m_szTagId + "\", \"" + szDate + "\")");
                }
            }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
            dp.show();
        }

    } // Class MyJavaScriptInterface


    @Override
    public boolean onCreateOptionsMenu (Menu menu){
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

    @Override
    public boolean onOptionsItemSelected (MenuItem item){
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

和Javascript代码是:



// ---- date picker to call methods of the android device
$('#datepicker').click(function(e){
	getDataDate();
});

function getDataDate(){
    MSSAndroidFunction.openDatePickerDialog('datepicker');
}


function callFromActivity_RetDate(datepicker, data) {
    document.subHours.datepicker.value = data;
}




1 个答案:

答案 0 :(得分:0)

您已将JavaScript接口命名为MyJavaScriptInterface,因此您必须从JavaScript调用它。

即:

而不是MSSAndroidFunction.openDatePickerDialog('datepicker'),您必须使用JavaScript方法执行MyJavaScriptInterface.openDatePickerDialog('datepicker')

我有一个基于WebView的应用程序,我在其中定义了另一个类中的接口,而不是内部类,并且在初始化中我做了:

htmlInterface=new HTMLInterface(this, webView);
webView.addJavascriptInterface(htmlInterface, "Android");

希望它能帮到你 此致