【问题标题】:How to call activity Android from button HTML?如何从按钮 HTML 调用活动 Android?
【发布时间】:2020-05-06 16:49:26
【问题描述】:

我想问一下如何从我的底部 HTML 调用 Android Studio 上的活动。

这是我的按钮 html 代码:

<button id="button" class="buttonHome" style="background:url(assets/scan.png)"></button>

这是我的java.activity:

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    this.architectView.onPostCreate();
    try {

 this.architectView.load("file:///android_asset/cobarealobjek/index.html");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这是我的.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.geowikitudes.RealObjectActivity">
    <com.wikitude.architect.ArchitectView
        android:id="@+id/architectView_real"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </com.wikitude.architect.ArchitectView>
</FrameLayout>

【问题讨论】:

    标签: javascript java android


    【解决方案1】:

    如果您可以选择使用 WebView 而不是 ArchitectView,您可能想尝试使用 JavascriptInterface

    您必须为您的界面定义一个类,然后将其绑定到您的 WebViewClient,例如,AppInterface

    import android.webkit.JavascriptInterface;
    import android.content.Context;
    import android.content.Intent;
    
    public class AppInterface {
        Context mContext;
        public AppInterface(Context context) {
            mContext = context;
        }
    
        // This is where you define the methods you want to be able to call from your web app
        // You have to annotate such methods with @JavascriptInterface
        @JavascriptInterface
        public void startMyActivity() {
            Intent intent = new Intent();
            intent.setClass(mContext, SecondActivity.class);  // Assuming SecondActivity is the name of activity you want to open
            mContext.startActivity(intent);
        }
    }
    

    在您的主要活动文件中,您可以有这样的内容:

    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    Webview webView;
    WebViewClient webVC;
    // ...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // ...
        webView = (WebView) findViewById(R.id.webview);  // Assuming you have a webview with the id of "webview" defined in your layout
        webVC = WebViewClient();
    
        webView.setWebViewClient(webVC);
        webView.getSettings().setJavascriptEnabled(true);
        webView.addJavascriptInterface(new AppInterface(this), "Android");  // You can set the second parameter to any other string, just make sure that you change it accordingly in your javascript
        webView.loadUrl("yourURL");
        // ...
    
    }
    

    最后,在您的 HTML/javascript 代码中,您需要通过提供的 javascript 接口调用该函数: &lt;button id="button" class="buttonHome" style="background:url(assets/scan.png)" onclick="Android.startMyActivity();"&gt;&lt;/button&gt;

    我希望这会有所帮助。

    【讨论】:

    • 没用,只显示白屏。可以结合 ArchitectView 和 WebView 吗?
    • 您可能需要通过添加 标签在 android 清单中声明互联网权限,因为 android 可能会阻止您的 WebView正在加载网页。
    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多