Python是動態語言,比較簡潔。Android不直接支援使用python開發應用,需要使用其它中介軟體或者庫。PythonForAndroid提供了在android平台上對python語言的支援;CLE支援python和java之間的互動,同時提供了一個通用的介面,可用於其它多種語言。Wrapandroid project將android類封裝為CLE對象,從而可以使多種語言可以調用android類。使用這三個組件,可以在android平台上直接使用python開發介面應用程式。Wrapandroid項目在進行中,目前的0.8.5版本已經提供了除SQlite,OpenGL之外大部分android類的封裝。完全可以編寫一個獨立的python應用程式。
在android上開發python應用程式,編程和打包環境可以直接使用eclipse。 1. 準備環境
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中。 2. 開始編程
a. 開啟eclipse, 建立一個新的android project “introduction”
b. 如果要從網路安裝,則需要添加許可許可權:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
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");
}
}
如果不希望從網路安裝CLE,可以將CLE的共用庫檔案包含在工程中,如下:
同時在activity中設定下載標誌為false
public 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"?>
<LinearLayout xmlns: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" >
<TextView
android:id="@+id/widget45"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
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"
/>
</LinearLayout>
f. 在assets目錄下,建立code.py檔案. 3. code.py代碼如下
a. 擷取當前服務
SrvGroup = libstarpy._GetSrvGroup()
Service = SrvGroup._GetService("","")
b. 擷取當前的Activity
StarActivity = Service.ActivityClass.getCurrent();
c. 擷取layout中定義的對象
MyText = StarActivity.findViewById("TextViewClass",StarActivity.getResource("id/widget45"));
findViewById函數與標準的android函數有差異,主要是輸入參數中增加了類的名稱;類名稱為android類名稱,加上”Class”尾碼。
MyText.setText("from layout");
MyButton = StarActivity.findViewById("ButtonClass",StarActivity.getResource("id/widget74"));
定義button對象的點擊事件
def MyButton_onClick(self, Ev) :
Service.ToastClass._New().makeText("Button is click", 0).show();
MyButton.onClick = MyButton_onClick;
MyButton.setOnClickListener();
setOnClickListener與android函數有差異,不需要輸入listener,事件觸發之後產生給本隊象。
d. 動態建立對象
擷取LinearLayout布局
MyLinearLayout = StarActivity.findViewById("LinearLayoutClass",StarActivity.getResource("id/widget73"));
建立一個動態按鈕
MyDynaButton = Service.ButtonClass._New(MyLinearLayout);
def MyDynaButton_onClick(self, Ev) :
Service.ToastClass._New().makeText("Button is click", 0).show();
MyDynaButton.onClick = MyDynaButton_onClick;
MyDynaButton.setOnClickListener();
MyDynaButton.setText("created dynamically");
設定按鈕的布局參數
MyDynaButton.setLinearLayoutParams(100,50);
運行結果如下:
例子下載:
http://wrapandroid-for-multilanguage.googlecode.com/svn/wiki/examples/pythongui85_introduction.rar