![](/img/trans.png)
[英]Phonegap on Android XmlHttpRequest Works in Emulator & browser, Not on Device
[英]webview date picker works in android emulator but not on device?
我已經使用這里發布的代碼示例從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; }
您已將JavaScript接口命名為MyJavaScriptInterface
,因此必須從JavaScript調用它。
即:
您必須在JavaScript方法中執行MyJavaScriptInterface.openDatePickerDialog('datepicker')
MSSAndroidFunction.openDatePickerDialog('datepicker')
,而不是MSSAndroidFunction.openDatePickerDialog('datepicker')
MyJavaScriptInterface.openDatePickerDialog('datepicker')
。
我有一個基於WebView
的應用程序,在其中我在另一個類中定義接口,而不是內部類,並在初始化中我做:
htmlInterface=new HTMLInterface(this, webView);
webView.addJavascriptInterface(htmlInterface, "Android");
希望它能幫到你
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.