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. 如果要從網絡安裝,則需要添加許可權限:
c. 將庫文件starcore_android_r6.jar和wrapandroid.jar到工程目錄下,將這兩個文件加入到工程中。
d. 編輯 IntroductionActivity.java,如下修改,加載python代碼import?com.srplab.wrapandroid.*;
public?class?IntroductionActivity?extends?WrapAndroidActivity?{
/**?Called?when?the?activity?is?first?created.?*/
@Override
public?void?onCreate(Bundle?savedInstanceState)?{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StarActivity._Call("DoAssetsFile",?"python",?"code.py");
}
}
同時在activity中設置下載標志為falsepublic?class?IntroductionActivity?extends?WrapAndroidActivity?{
/**?Called?when?the?activity?is?first?created.?*/
@Override
public?void?onCreate(Bundle?savedInstanceState)?{
DownloadFromNetFlag?=?false;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StarActivity._Call("DoAssetsFile",?"python",?"code.py");
}
}
只不過此時生成的安裝包大一些。
e. 編輯layout:main.xml.<?xml ?version="1.0"?encoding="utf-8"?>
android:id="@+id/widget73"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"?>
android:id="@+id/widget45"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"?/>
android:id="@+id/widget74"
android:layout_width="220dp"
android:layout_height="48dp"
android:text="thank?for?your?use"
android:typeface="serif"
android:textStyle="bold"
android:textColor="#ffff0000"
android:layout_x="284dp"
android:layout_y="220dp"
android:textSize="16dp"
/>
f. 在assets目錄下,創建code.py文件.
推薦學習:《Python教程》
總結
以上是生活随笔為你收集整理的python可以编写成手机吗_python可以编写手机应用吗的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Python控制手机(一)
- 下一篇: 中国式危机公关9加1策略(第七章 道歉先