在Cordova中单击html按钮时启动新活动

时间:2021-01-30 20:43:14

I am trying to build Android Application with Cordova and Java. I want to build intent that start a new Intent when button clicked in the HTML code.

我正在尝试使用Cordova和Java构建Android应用程序。我希望在HTML代码中单击按钮时构建启动新Intent的intent。

This is my HTML code:

这是我的HTML代码:

<!DOCTYPE html>
<html>
<head>
    <title>Demo Phonegap</title>
    <script type="text/javascript" charset="utf-8" src="cordova.js">
    </script>
</head>
<body>
<h2>Hello Android</h2>
<button onclick="openNewActivity()">test</button>
</body>
</html>

And this is my Java code:

这是我的Java代码:

public class MyActivity extends DroidGap {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        super.loadUrl("file:///android_asset/www/index.html");
    }

    public void NewIntent()
    {
        Intent i = new Intent(this,HelpActivity.class);
        startActivity(i);
    }
}

How can I call the NewIntent() void from the html file without building plugin.

如何在不构建插件的情况下从html文件中调用NewIntent()void。

Thanks for answers Haim.

谢谢你的回答Haim。

EDIT:I using the phonegap for the GUI of my application.I want to call to another activity when button is clicked in the cordova html file.

编辑:我使用phonegap作为我的应用程序的GUI。我想在cordova html文件中单击按钮时调用另一个活动。

2 个解决方案

#1


4  

I don't know why you need to start an activity in DroidGap. But if you want to perform some native android code on Html's button click, you can use this approach.

我不知道为什么你需要在DroidGap中开始一个活动。但是如果你想在Html的按钮点击上执行一些原生的android代码,你可以使用这种方法。

You will have to use JavascriptInterface for WebView.

您将不得不使用JavascriptInterface for WebView。

Example code snippet:

示例代码段:

//in onCreate 
appView.addJavascriptInterface(new MyJSInterface(),
            "myJSInterface");
super.loadUrl("file:///android_asset/www/index.html");


//JSInterface class
public class MyJSInterface {

    public MyJSInterface() {
        // TODO Auto-generated constructor stub
        Log.i(TAG, "constructor of jsinterface");
    }

    public void btnClick () {
        //do something
    }
}

And in your html:

在你的HTML中:

<button onclick="callFunction()">test</button>

In your js:

在你的js:

function callFunction(){
    myJSInterface.btnClick();
}

Hope this helps.

希望这可以帮助。

#2


-2  

You'll need to create the button in java. An example is below.

您需要在java中创建按钮。一个例子如下。

Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 Intent i = new Intent(this,HelpActivity.class);
    startActivity(i);

             }
         });
     }

#1


4  

I don't know why you need to start an activity in DroidGap. But if you want to perform some native android code on Html's button click, you can use this approach.

我不知道为什么你需要在DroidGap中开始一个活动。但是如果你想在Html的按钮点击上执行一些原生的android代码,你可以使用这种方法。

You will have to use JavascriptInterface for WebView.

您将不得不使用JavascriptInterface for WebView。

Example code snippet:

示例代码段:

//in onCreate 
appView.addJavascriptInterface(new MyJSInterface(),
            "myJSInterface");
super.loadUrl("file:///android_asset/www/index.html");


//JSInterface class
public class MyJSInterface {

    public MyJSInterface() {
        // TODO Auto-generated constructor stub
        Log.i(TAG, "constructor of jsinterface");
    }

    public void btnClick () {
        //do something
    }
}

And in your html:

在你的HTML中:

<button onclick="callFunction()">test</button>

In your js:

在你的js:

function callFunction(){
    myJSInterface.btnClick();
}

Hope this helps.

希望这可以帮助。

#2


-2  

You'll need to create the button in java. An example is below.

您需要在java中创建按钮。一个例子如下。

Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 Intent i = new Intent(this,HelpActivity.class);
    startActivity(i);

             }
         });
     }