python可不可以用来编写手机应用
这篇文章将为大家详细讲解有关python可不可以用来编写手机应用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
Python是动态语言,比较简洁。Android不直接支持使用python开发应用,需要使用其它中间件或者库。PythonForAndroid提供了在android平台上对python语言的支持;CLE支持python和java之间的交互,同时提供了一个通用的接口,可用于其它多种语言。
Wrapandroid project将android类封装为CLE对象,从而可以使多种语言可以调用android类。使用这三个组件,可以在android平台上直接使用python开发界面应用程序。
在android上开发python应用程序,编程和打包环境可以直接使用eclipse。
准备环境
a: 安装PythonForAndroid: http://code.google.com/p/android-scripting
b: CLE在程序运行的时候,自动从网络上安装,也可以下载库函数,放到工程中。开发需要java库文件starcore_android_r6.jar。文件在starcore_devfiles_r6.zip中,可以从http://code.google.com/p/cle-for-android下载
c: Wrapandroid项目库文件wrapandroid.jar,包含在压缩包http:/code.google.com/p/wrapandroid-for-multilaguage/download/wrapandroid_devfiles_0_8_6.rar中。
开始编程
a. 打开eclipse, 创建一个新的android project “introduction”
b. 如果要从网络安装,则需要添加许可权限:
<uses-permissionandroid:name="android.permission.INTERNET"/><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
c. 将库文件starcore_android_r6.jar和wrapandroid.jar到工程目录下,将这两个文件加入到工程中。
d. 编辑 IntroductionActivity.java,如下修改,加载python代码
importcom.srplab.wrapandroid.*;publicclassIntroductionActivityextendsWrapAndroidActivity{/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);StarActivity._Call("DoAssetsFile","python","code.py");}}
同时在activity中设置下载标志为false
publicclassIntroductionActivityextendsWrapAndroidActivity{/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){DownloadFromNetFlag=false;super.onCreate(savedInstanceState);setContentView(R.layout.main);StarActivity._Call("DoAssetsFile","python","code.py");}}
只不过此时生成的安装包大一些。
e. 编辑layout:main.xml.
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/widget73"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:id="@+id/widget45"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/><Buttonandroid:id="@+id/widget74"android:layout_width="220dp"android:layout_height="48dp"android:text="thankforyouruse"android:typeface="serif"android:textStyle="bold"android:textColor="#ffff0000"android:layout_x="284dp"android:layout_y="220dp"android:textSize="16dp"/></LinearLayout>
f. 在assets目录下,创建code.py文件.
关于python可不可以用来编写手机应用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。