日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

cordova自定义一个简单的alert的插件(android平台)

發(fā)布時間:2025/3/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cordova自定义一个简单的alert的插件(android平台) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

官網(wǎng)的插件開發(fā)文檔:http://cordova.apache.org/docs/en/latest/guide/hybrid/plugins/index.html

插件文件布局:

比如我在d盤新建了如下的布局

--src

????--android

????????--alert.java

--www

????--alert.js

plugin.xml

插件功能:

在前端點擊一個按鈕,提示alert.

文件內容:

????關于plugin.xml文件內容設置可以參考這里http://cordova.apache.org/docs/en/latest/plugin_ref/spec.html

現(xiàn)在我的內容如下:

<?xml?version="1.0"?encoding="UTF-8"?> <!--Licensed?to?the?Apache?Software?Foundation?(ASF)?under?oneor?more?contributor?license?agreements.??See?the?NOTICE?filedistributed?with?this?work?for?additional?informationregarding?copyright?ownership.??The?ASF?licenses?this?fileto?you?under?the?Apache?License,?Version?2.0?(the"License");?you?may?not?use?this?file?except?in?compliancewith?the?License.??You?may?obtain?a?copy?of?the?License?athttp://www.apache.org/licenses/LICENSE-2.0Unless?required?by?applicable?law?or?agreed?to?in?writing,software?distributed?under?the?License?is?distributed?on?an"AS?IS"?BASIS,?WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANYKIND,?either?express?or?implied.??See?the?License?for?thespecific?language?governing?permissions?and?limitationsunder?the?License. --><plugin?xmlns="http://apache.org/cordova/ns/plugins/1.0"id="alert-plugin"version="1.0.0"><name>AlertPlugin</name><description>顯示alert的插件</description><license>Apache?2.0</license><keywords>cordova,AlertPlugin</keywords><engines><engine?name="cordova-android"?version=">=3.6.0"?/><!--?Requires?CordovaPlugin.preferences?--></engines><js-module?src="www/alert.js"?><clobbers?target="navigator.alert"?/></js-module><!--?android?--><platform?name="android"><config-file?target="res/xml/config.xml"?parent="/*"><feature?name="alert"><param?name="android-package"?value="com.alert"/><param?name="onload"?value="true"/></feature></config-file><source-file?src="src/android/alert.java"?target-dir="src/com"?/></platform> </plugin>

基礎的不說,說下js-module,關于<clobbers>官網(wǎng)上是這樣說的

  • <clobbers?target="some.value"/>?indicates that the?module.exports?is inserted into the?windowobject as?window.some.value. You can have as many?<clobbers>?as you like. Any object not available on?window?is created.

意思是,比如我www下的alert.js文件如下

var?exec?=?require('cordova/exec');var?alertPlugin?=?{show:function(success,error,message)?{exec(success,?error,?"alert",?"show",?message);} };module.exports?=?alertPlugin;

如果我這樣配置的話

<js-module?src="www/alert.js"?><clobbers?target="navigator.alert"?/></js-module>

意味著

navigator.alert就代表了alertPlugin

當我們調用alertPlugin.show方法的時候就等同于調用navigator.alert.show方法就行。

<source-file?src="src/android/alert.java"?target-dir="src/com"?/>

這個指明了核心類的名稱,以及當android項目執(zhí)行安裝的時候,alert.java是會出現(xiàn)在src/com路徑下的


?<feature?name="alert"><param?name="android-package"?value="com.alert"/><param?name="onload"?value="true"/></feature>

value="com.alert"對應了android項目下的com.alert.java文件

<feature name="alert">中的alert和

?cordova.exec(function(winParam)?{},?function(error)?{},?"service",?"action",?["firstArgument",?"secondArgument",?42,?false]);

中的service名字一樣


對于alert.java,內容如下

public?class?alert?extends?CordovaPlugin?{@Overridepublic?boolean?execute(String?action,?JSONArray?args,?CallbackContext?callbackContext)?throws?JSONException?{if?(action.equals("show"))?{String?message?=?args.getString(0);this.echo(message,?callbackContext);return?true;}return?false;}private?void?echo(String?message,?CallbackContext?callbackContext)?{if?(message?!=?null?&&?message.length()?>?0)?{AlertDialog.Builder?builder?=?new?Builder(cordova.getActivity());builder.setMessage(message);builder.setTitle("提示");callbackContext.success(message);}?else?{callbackContext.error("Expected?one?non-empty?string?argument.");}} }

注意show是和

cordova.exec(function(winParam)?{},?function(error)?{},?"service",?"action",?["firstArgument",?"secondArgument",?42,?false]);


中的action名字是一樣的。

插件分享在這個地址:

http://www.oschina.net/code/snippet_778987_53062

轉載于:https://my.oschina.net/liangzhenghui/blog/549018

總結

以上是生活随笔為你收集整理的cordova自定义一个简单的alert的插件(android平台)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。