我在Phonegap www文件夹中有用户注册页面。我想使用原生Android功能验证电子邮件ID和电话号码。我该怎么办?
答案 0 :(得分:0)
我们必须编写JS接口来从javascript执行java代码。
在onCreate方法中写下以下代码:
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new Object()
{
public void performClick(String emailAdress, String phoneNo)
{
// Validate email address and Phone number here
}
},"MyAndroid");
}
}
要在phonegap app中调用JS接口,请编写javascript函数:
<script>
document.getElementById("validateButton").addEventListener('touchstart',touchPagePressed);// you can use 'onclick' also
function touchPagePressed()
{
// MyAndroid interface will execute java's performClick method
MyAndroid.performClick($("#emailId").val(),$("#phoneNo").val());
}
</script>