简体   繁体   English

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

[英]webview date picker works in android emulator but not on device?

I've used the code example posted here calling date picker from javascript and this works fine in the android emulator. 我已经使用这里发布的代码示例从javascript调用日期选择器 ,这在android模拟器中工作正常。 When I click on the date input box in web view, the android date picker appears and I can select it. 当我在Web视图中单击日期输入框时,会出现android日期选择器,我可以选择它。

However, when I test this on my device, I just get a standard text input field it is as though the method is not being called. 但是,当我在我的设备上测试它时,我只是得到一个标准的文本输入字段,就好像该方法没有被调用一样。 Does anyone have any suggestions? 有没有人有什么建议? The full code I am using is: 我使用的完整代码是:

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

and the Javascript code is: 和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; } 

You have named your JavaScript Interface as MyJavaScriptInterface , so you must call it from JavaScript. 您已将JavaScript接口命名为MyJavaScriptInterface ,因此必须从JavaScript调用它。

Ie: 即:

Instead of MSSAndroidFunction.openDatePickerDialog('datepicker') , you must do MyJavaScriptInterface.openDatePickerDialog('datepicker') , in a JavaScript method. 您必须在JavaScript方法中执行MyJavaScriptInterface.openDatePickerDialog('datepicker') MSSAndroidFunction.openDatePickerDialog('datepicker') ,而不是MSSAndroidFunction.openDatePickerDialog('datepicker') MyJavaScriptInterface.openDatePickerDialog('datepicker')

I have a WebView based application, in which I define the Interface in another class, not an inner class, and in the initialization I do: 我有一个基于WebView的应用程序,在其中我在另一个类中定义接口,而不是内部类,并在初始化中我做:

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

Hope it helps you 希望它能帮到你
Regards 问候

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android XmlHttpRequest上的Phonegap可在模拟器和浏览器中使用,而不是在设备上使用 - Phonegap on Android XmlHttpRequest Works in Emulator & browser, Not on Device Cordova 相机插件适用于模拟器,而不适用于 Android 设备 - Cordova camera plugin works on emulator and not on Android device jQuery Date时间选择器,在iOS / Android上运行良好 - jQuery Date Time picker that works well on iOS/Android RouterLink 在浏览器中工作,但不能在模拟器或设备上工作 - RouterLink Works in Browser But Not on Emulator or Device Android WebView-在浏览器中有效,但在WebView中无效 - Android WebView - Works in browser but not WebView 我从模拟器上的webview获取源代码,但未在真实设备上获取? -安卓 - I'm getting source code from webview on emulator, but not getting on real device ? - Android Phonegap:我的Javascript代码可在浏览器上使用,但不能在Android模拟器或设备上使用 - Phonegap: My Javascript code works on browsers but it doesn't work on Android emulator or device 简单的javascript可在intel xdk模拟器中使用,但不能在设备上使用 - simple javascript works in intel xdk emulator but not on device Intel XDK代码可在模拟器上运行,但不能在真实设备上运行 - Intel XDK code works on emulator but not on real device Reactjs 托管的 PWA 在 android 设备上显示空白页,并且在桌面上工作正常。当我添加 webview 时? - Reactjs hosted PWA displays blank page on android device and works fine on desktop .when I added webview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM