简体   繁体   English

Android webapp,加载另一个 HTML

[英]Android webapp, loading another HTML

I'm creating an Android webapp for a simple login\register using WebView.我正在使用 WebView 为简单的登录\注册创建一个 Android webapp。 My HTML page have a register button (on login.html) that should take the user from the login page to the register page.我的 HTML 页面有一个注册按钮(在 login.html 上),应该将用户从登录页面带到注册页面。

My issue is that no matter what method I use to go from pages, nothing work.我的问题是,无论我使用什么方法从页面到 go,都没有任何效果。

My login.HTML:我的登录名.HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery-3.5.1.js"></script>
</head>
<body>
<link rel="stylesheet" href="css/style.css">
<form action="index.html" method="post">
    <div class="imgcontainer">
    </div>
    <h1>Login</h1>
    <div class="container">
        <label><b>Username</b></label>
        <input type="text" placeholder="Enter Username" name="uname" required>

        <label><b>Password</b></label>
        <input type="password" placeholder="Enter Password" name="psw" required>

        <button type="submit">Login</button>
        <label>
            <input type="checkbox" checked="checked" name="remember"> Remember me
        </label>
    </div>
    <div class="container">
        <span><p>New User?</p>
        <button type="button" id="regbtn">Register</button>
        </span>
    </div>
</form>
<script>
         $("#regbtn").on("click",function(){
             app.goToRegister();
         });

</script>
</body>
</html>

The way I've create the goToRegister() :我创建goToRegister()的方式:

    @JavascriptInterface
    public void goToRegister() {
        context.goToRegister();
    }
    public void goToRegister(){
        String reg = "register.html";
        //webView.loadUrl("javascript:myFunction(\""+reg+"\")");
        webView.loadUrl("file:///android_asset/register.html");
    }

I had implemented the same method on my function.js and as a function within login.HTML , neither worked.我在我的function.js和 login.HTML 中的login.HTML上实现了相同的方法。 Ofcourse when I tried to use the method from function.js the button of the HTML was different.当然,当我尝试使用 function.js 中的方法时, function.js的按钮是不同的。

Both page are on the same local project.两个页面都在同一个本地项目上。 When I click the button, nothing happens.当我单击按钮时,什么也没有发生。

EDIT:编辑:

I have all the required javascript options enabled:我启用了所有必需的 javascript 选项:

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);


        webViewJavaScriptInterface = new WebViewJavaScriptInterface(this);
        webView.addJavascriptInterface(webViewJavaScriptInterface,"app");
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        WebView.setWebContentsDebuggingEnabled(true);

You have this error:你有这个错误:

 @JavascriptInterface
    public void goToRegister() {
        context.goToRegister();
    }

here "context" dose not have "goToRegister()" method declared.这里“上下文”没有声明“goToRegister()”方法。

public class WebAppInterface {
    MainActivity mActivity;


    WebAppInterface(MainActivity a) {
        mActivity = a;
    }


  @JavascriptInterface
    public void goToRegister() {
        mActivity.goToRegister();
    }
}

Hope it works... Best of luck.希望它有效......祝你好运。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM