gpt4 book ai didi

android - Toast 消息不会出现在 android studio phonegap 中

转载 作者:太空狗 更新时间:2023-10-29 14:02:02 26 4
gpt4 key购买 nike

我是 android studio 的新手。我正在为我的应用程序做 toast 消息,但我无法执行此操作。这个在 eclipse 中工作,但不适用于 android studio。我已经尝试了很多,但 toast 消息根本没有显示。下面是代码。请建议我。提前谢谢你。

索引.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="toastPlugin.js"></script>
<script>
function toast()
{
// alert('hii');
// shortToast("Short Toast Message Here...");
longToast("Long Toast Message Here...");
}
</script>

</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<div onclick="toast();">click</div>

</div>
</div>
</body>
</html>

toastPlugin.js

          window.shortToast = function(str, callback) {   
cordova.exec(callback, function(err) {
callback('Nothing to echo.');
}, "ToastPlugin", "shortToast", [ str ]);
};

window.longToast = function(str, callback) {
alert(str);//Long Toast Message Here...
cordova.exec(callback, function(err) { alert("Nothing to echo");
callback('Nothing to echo.');
}, "ToastPlugin", "longToast", [ str ]);
};

ToastPlugin.java

package com.raddyx.myapplication;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.util.Log;
import android.widget.Toast;

public class ToastPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {

String message = args.getString(0);

// used to log the text and can be seen in LogCat
Log.d("Toast Plugin", "Calling the Toast...");
Log.d("Toast Plugin", message);

if (action.equals("shortToast")) {
this.shortToast(message, callbackContext);
return true;
} else if (action.equals("longToast")) {
this.longToast(message, callbackContext);
return true;
}
return false;
}

private void shortToast(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
Toast.makeText(cordova.getActivity().getApplicationContext(),
message, Toast.LENGTH_SHORT).show();
callbackContext.success(message);
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}

private void longToast(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
Toast.makeText(cordova.getActivity().getApplicationContext(),
message, Toast.LENGTH_LONG).show();
callbackContext.success(message);
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}
}

在config.xml中

 <feature name="ToastPlugin"> <param name="android-package" value="com.raddyx.plugins.ToastPlugin" /> </feature>

最佳答案

PhoneGap Toast 插件

使用 CLI 为 iOS、Android 和 WP8 Cordova 应用程序安装 toast 弹出插件

cordova plugin add cordova-plugin-x-toast

然后在你的index.html中添加cordova_plugins.js并调用下面的函数

showShortTop(消息)

window.plugins.toast.showShortTop('Hello there!', function(a) {
console.log('toast success: ' + a);
}, function(b) {
alert('toast error: ' + b);
})

showLongBottom(消息)

window.plugins.toast.showLongBottom('Hello there!', function(a) {
console.log('toast success: ' + a)
}, function(b) {
alert('toast error: ' + b)
})

关于android - Toast 消息不会出现在 android studio phonegap 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34893998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com