[英]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.