0
我正在開發一個Android應用程序。我需要從Web視圖中調用一個android功能。我從互聯網上找到一個代碼,使用JavaScript從HTML頁面調用android toast消息。該代碼與HTML頁面正常工作,但不工作時,我使用的是一個asp.net web窗體。在這裏,我與你分享該代碼。請告訴我如何使用asp.net web窗體實現這個過程。在此先感謝Android的JavaScript接口與asp.net
代碼的Android活動:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.web_view);
webView.loadUrl("file:///android_res/drawable/mypage.html");
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app");
}
public class WebViewJavaScriptInterface{
private Context context;
public WebViewJavaScriptInterface(Context context){
this.context = context;
}
@JavascriptInterface
public void makeToast(String message, boolean lengthLong){
Toast.makeText(context, message, (lengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT)).show();
}
}
}
活動的佈局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
HTML頁:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript View</title>
<script type="text/javascript">
function showToast(){
var message = document.getElementById("message").value;
var lengthLong = document.getElementById("length").checked;
app.makeToast(message, lengthLong);
return false;
}
window.onload = function(){
var form = document.getElementById("form");
form.onsubmit = showToast;
}
</script>
</head>
<body>
<form id="form">
Message: <input id="message" name="message" type="text"/><br />
Long: <input id="length" name="length" type="checkbox" /><br />
<input type="submit" value="Make Toast" />
</form>
</body>
</html>
該代碼與html頁面正常工作,但我需要在asp.net中實現此過程。我在我的asp.net web表單中嘗試了上面的java腳本,但它不起作用。任何建議? –
嘗試在最後添加load ur。 Asp.net或PHP或任何它沒有關係我猜。嘗試添加日誌n中斷點檢查您的方法是否被調用 – Sush
您是否發現此問題? – Sush